获取不同类型的数据源:
对于看上的网站如何获取其信息:
1.分析原网站是如何获取到这些数据的?哪个接口?哪些参数?
2.用程序去调用接口(python/java都可以)
3.处理一些数据,优化数据传入数据库
java爬虫操作流程:
先创建一个实体类:根据网络上需要操作的请求的属性规定实体类属性一一对应。
爬取:https://www.code-nav.cn/learn/passage
数据抓取的几种方式:
1.直接去请求接口(最方便)HttpClient,OkHttp,Hutool,resttemplate
2.等网页渲染出明文内容后,从前端页面的内存抓取
3.有一些网站可能是动态请求的,他不会一次性加载所有数据,而是要你点击某个按钮,输入某个验证码后才会显示出数据。 => 无头浏览器(后台代替开启浏览器)比如:java的selenium和nodejs的puppeteer
数据抓取流程:
1.分析数据源(怎么获取)
2.拿到数据后,怎么处理?
3.写入数据库等存储
1.方式一:使用okhttp
1.引入依赖
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.1.0</version>
</dependency>
2.构造请求
Okhttp3
完成页面请求,需要三大步骤:
- 实例化
OkHttpClient
- 执行调用。
- 在执行调用之前,需要实例化一个
Request
对象 - 然后构建调用对象
- 最后执行调用,如果调用失败可能抛异常,所以必须抓取异常
- 在执行调用之前,需要实例化一个
- 调用对象的方法即可获取返回的字符串内容
get请求
public class Main {
public static void main(String[] args) throws IOException {
String url = "https://4399.com";
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Call call = okHttpClient.newCall(request);
String string = call.execute().body().string();
System.out.println(string);
}
}
post请求
public static final MediaType JSON_TYPE = MediaType.parse("application/json; charset=utf-8");
/**
* 向指定的 url 提交数据,以 json 的方式
*/
public String postContent(String url, Map<String, String> datas) {
// okHttpClient 实例
OkHttpClient okHttpClient = new OkHttpClient();
// 数据对象转换成 json 格式字符串
String param = JSON.toJSONString(datas);
//post方式提交的数据
RequestBody requestBody = RequestBody.create(JSON_TYPE, param);
Request request = new Request.Builder().url(url).post(requestBody).build();
// 使用client去请求
Call call = okHttpClient.newCall(request);
// 返回结果字符串
String result = null;
try {
// 获得返回结果
result = call.execute().body().string();
} catch (IOException e) {
// 抓取异常
System.out.println("request " + url + " error . ");
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
String url = "https://4399.com";
Map<String, String> datas = new HashMap();
datas.put("num", "6666");
Main poster = new Main();
String content = poster.postContent(url, datas);
System.out.println("API调用结果");
System.out.println(content);
}
2.方式二:使用Hutool
<!-- https://hutool.cn/docs/index.html#/-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.8</version>
</dependency>
public static void main(String[] args) throws IOException {
String json = "{\n" +
" \"current\": 1,\n" +
" \"pageSize\": 8,\n" +
" \"sortField\": \"createTime\",\n" +
" \"sortOrder\": \"descend\",\n" +
" \"category\": \"文章\",\n" +
" \"tags\": [],\n" +
" \"reviewStatus\": 1\n" +
"}";
String url = "https://4399.com";
String result2 = HttpRequest.post(url)
.body(json)
.execute().body();
String uu = "F:\\user-center-backend\\src\\main\\java\\com\\yupi\\usercenter";
File file = new File(uu, "result.json");
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(result2);
System.out.println(result2);
}
3.方式三:使用Jsoup
1.引入依赖
<!-- jsoup-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.15.3</version>
</dependency>
2.构造请求
package org.example.cetidenet;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.example.cetidenet.model.entity.Post;
import org.example.cetidenet.service.PostService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
@SpringBootTest
class CetideNetApplicationTests {
@Autowired
private PostService postService;
void testFetchPic() throws IOException {
int current = 1;
String url = "https://4399.com";
Document doc = Jsoup.connect(url).get();
Elements elements = doc.select(".iuscp.isv");
for(Element h : elements){
//取图片地址(murl)
String m = h.select(".iusc").get(0).attr("m");
//取地址
Map<String,Object> result = JSONUtil.toBean(m,Map.class);
String murl = (String)result.get("murl");
System.out.println(murl);
String title =h.select(".inflnk").get(0).attr("aria-label");
System.out.println(title);
}
}
}