日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Jsoup爬虫的基本使用

發(fā)布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Jsoup爬虫的基本使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

什么是Jsoup?

jsoup 是一款Java 的HTML解析器,可直接解析某個URL地址、HTML文本內(nèi)容。它提供了一套非常省力的API,可通過DOM,CSS以及類似于jQuery的操作方法來取出和操作數(shù)據(jù)(簡稱爬蟲)。

基本使用

新建一個maven項目

<dependencies><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.0.1</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.0.1</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId><version>4.0.1</version></dependency><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.4</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1.1</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>1.4</version></dependency><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.11.3</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.1</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>compile</scope></dependency> </dependencies>

測試類

@Testpublic void test111() throws Exception{ // 1、爬取的urlString targetUrl = "https://zhipeng0908.gitee.io"; // 2、獲取connection,CrawlerUtil工具類在下方Connection connect = CrawlerUtil.getConnection(targetUrl); // 4、執(zhí)行Connection.Response response = connect.method(Connection.Method.GET).execute(); // 5、處理爬蟲結(jié)果 // 得到domDocument document = response.parse(); // <body></body>Element bodyElement = document.body();// .post-header為這個html中一個div的類名 // Elements 類繼承了ArrayList類Elements cardElement = bodyElement.select(".post-header"); // 處理結(jié)果,獲得文本內(nèi)容for (Element blog : cardElement) {Elements titleElement = blog.select(".post-title");String title = titleElement.text();Elements timeElement = blog.select(".post-meta > span.post-time > time");String time = timeElement.text();Elements linkElement = blog.select(".post-title-link");String link = linkElement.attr("href");System.out.println("博客標(biāo)題:"+title + "\t" + "url:" + (targetUrl+link) + "\t"+"發(fā)布時間:"+time);}}

工具類

public static Connection getConnection(String targetUrl){Connection connect = Jsoup.connect(targetUrl); // 3、偽造請求頭connect.header("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");connect.header("Accept-Encoding","gzip, deflate, br");connect.header("Accept-Language","zh-CN,zh;q=0.9");connect.header("Cache-Control","no-cache");connect.header("Connection","keep-alive");connect.header("Cookie","_ga=GA1.2.2130438396.1588431092; Hm_lvt_ec661610f14acf2457496da3a87d804d=1588840665,1589378478; Hm_lpvt_ec661610f14acf2457496da3a87d804d=1589378528");connect.header("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36");return connect;}

結(jié)果

總結(jié)

以上是生活随笔為你收集整理的Jsoup爬虫的基本使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。