迅速提高网站排名,网站弹出咨询这个怎么做,wordpress 更改地址,淘宝做网站价格文章目录 三种抓取方式数据抓取的流程获取文章具体操作 获取用户获取图片jsoup操作 三种抓取方式
直接调用请求接口(最方便#xff0c;这里使用该方法) HttpClient,OKHttp,RestTemplate,Hutool等网页渲染出明文内容后#xff0c;从前端页面的内容抓取有些网站可能是动态请求… 文章目录 三种抓取方式数据抓取的流程获取文章具体操作 获取用户获取图片jsoup操作 三种抓取方式
直接调用请求接口(最方便这里使用该方法) HttpClient,OKHttp,RestTemplate,Hutool等网页渲染出明文内容后从前端页面的内容抓取有些网站可能是动态请求的不会一次性加载所有的数据而是要你点击某个按钮输入某个验证码才会显示出数据 - 无头浏览器
数据抓取的流程
分析数据源怎么获取拿到数据后怎么处理写入数据库等存储
获取文章
离线抓取方式
具体操作
过滤请求 将响应的数据复制到data/passage.json文件中 引入Hutool依赖
!-- https://hutool.cn/docs/index.html#/ --
dependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.8.16/version
/dependency查看官方文档
https://hutool.cn/docs/index.html#/
编写测试类
获取用于爬虫的数据 编写测试类
SpringBootTest
public class CrawlerTest {Resourceprivate PostService postService;Testvoid testFetchPassage() {//1.获取数据String json {\current\:1,\pageSize\:8,\sortField\:\createTime\,\sortOrder\:\descend\,\category\:\文章\,\tags\:[],\reviewStatus\:1};String url https://api.code-nav.cn/api/post/search/page/vo;String result HttpRequest.post(url).body(json).execute().body();//2.处理数据:json转对象MapString, Object map JSONUtil.toBean(result, Map.class);JSONObject data (JSONObject) map.get(data);JSONArray records (JSONArray) data.get(records);ListPost postList new ArrayList();for (Object record : records) {Post post new Post();JSONObject tempRecord (JSONObject) record;post.setId(0L);post.setTitle(tempRecord.getStr(title));post.setContent(tempRecord.getStr(content));//这里将json数组转为列表再转为json字符串不知道是为了干什么为什么不直接把json数组转为json字符串呢JSONArray tags (JSONArray) tempRecord.get(tags);ListString tagList tags.toList(String.class);// System.out.println(JSONUtil.toJsonStr(tags));post.setTags(JSONUtil.toJsonStr(tagList));System.out.println(JSONUtil.toJsonStr(tagList));post.setUserId(1L);postList.add(post);//3.写入数据库postService.saveBatch(postList);}}
}
写入一次性任务
// 取消Component注释每次项目启动都会执行run任务
//Component
Slf4j
public class FetchInitPostList implements CommandLineRunner {Resourceprivate PostService postService;Overridepublic void run(String... args) {//1.获取数据String json {\current\:1,\pageSize\:8,\sortField\:\createTime\,\sortOrder\:\descend\,\category\:\文章\,\tags\:[],\reviewStatus\:1};String url https://api.code-nav.cn/api/post/search/page/vo;String result HttpRequest.post(url).body(json).execute().body();//2.处理数据:json转对象MapString, Object map JSONUtil.toBean(result, Map.class);JSONObject data (JSONObject) map.get(data);JSONArray records (JSONArray) data.get(records);ListPost postList new ArrayList();for (Object record : records) {Post post new Post();JSONObject tempRecord (JSONObject) record;post.setId(0L);post.setTitle(tempRecord.getStr(title));post.setContent(tempRecord.getStr(content));//这里将json数组转为列表再转为json字符串不知道是为了干什么为什么不直接把json数组转为json字符串呢JSONArray tags (JSONArray) tempRecord.get(tags);ListString tagList tags.toList(String.class);//JSONUtil.toJsonStr(tags);post.setTags(JSONUtil.toJsonStr(tagList));post.setUserId(1L);postList.add(post);//3.写入数据库postService.saveBatch(postList);}}
}
获取用户
每个网站的用户都是自己的没必要抓取
获取图片
实时抓取我们自己的网站不存在这些数据用户要搜的时候直接从别人的接口网站去搜 jsoup库获取到HTML文档然后从中解析出需要的字段
jsoup操作
导入依赖
!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --
dependencygroupIdorg.jsoup/groupIdartifactIdjsoup/artifactIdversion1.15.3/version
/dependency打开官网 https://jsoup.org/获取示例代码
Document doc Jsoup.connect(https://en.wikipedia.org/).get();
log(doc.title());
Elements newsHeadlines doc.select(#mp-itn b a);
for (Element headline : newsHeadlines) {log(%s\n\t%s, headline.attr(title), headline.absUrl(href));
}通过在网页前端html界面找对应的css选择器拿到需要的内容测试代码 //抓取图片Testvoid testFetchPicture() throws IOException {int current 1;String url https://cn.bing.com/images/search?q%E5%B0%8F%E9%BB%91%E5%AD%90formHDRSC2first current;Document doc Jsoup.connect(url).get();Elements elements doc.select(.iuscp.isv); //数组每个元素是每一张图片for (Element element : elements) {//取图片地址murlString m element.select(.iusc).attr(m);MapString,Object map JSONUtil.toBean(m, Map.class);String murl (String) map.get(murl);//取标题String title element.select(.inflnk).attr(aria-label);System.out.println(murl);System.out.println(title);}}