更新時間:2021-11-12 16:52:11 來源:動力節點 瀏覽1565次
今天我們就來說說關于Java網絡爬蟲的介紹。在本文中,我們以虎撲榜的新聞標題和詳情頁為例。我們需要提取的內容如下:
我們需要提取帶圓圈的文本及其對應的鏈接。在提取的過程中,我們會使用兩種方式提取,一種是Jsoup,一種是httpclient+正則表達式。這也是Java網絡爬蟲常用的兩種方式。你不知道這兩種方式是無關緊要的。后面會有相應的手冊。在正式編寫提取程序之前,先講解一下Java爬蟲系列博文的環境。本系列博文中的所有demo都是使用SpringBoot搭建的。無論您使用什么環境,您只需要正確導入相應的包即可。
基于Jsoup的信息抽取
首先創建一個隨機名稱的Springboot項目,并在pom.xml中引入Jsoup依賴
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.12.1</version>
</dependency>
好的,我們一起來分析一下頁面。你還沒有瀏覽它。在列表頁面中,我們使用 F12 評論元素來查看頁面結構。經過我們的分析,我們發現列表新聞在<div class="news-list">標簽下,每條新聞都是一個li標簽。分析結果如下:
因為之前我們已經知道了css選擇器,所以我們編譯了我們a標簽的css選擇器的代碼:Div。新聞列表 > UL > Li > Div. list-hd > H4 > a,結合瀏覽器的opy功能。全部都準備好了。我們一起編譯了Jsoup模式提取信息的代碼:
/**
* jsoup Ways to Get Tiger Pop News List Page
* @param url Hupu News List page url
*/
public void jsoupList(String url){
try {
Document document = Jsoup.connect(url).get();
// Using css selector to extract list news a tag
// <a target="_blank">Howard: I had a 30-day diet during the summer break, which tested my mind and body.</a>
Elements elements = document.select("div.news-list > ul > li > div.list-hd > h4 > a");
for (Element element:elements){
// System.out.println(element);
// Get Details Page Links
String d_url = element.attr("href");
// Get the title
String title = element.ownText();
System.out.println("Details page links:"+d_url+" ,Details page title:"+title);
}
} catch (IOException e) {
e.printStackTrace();
}
}
使用Jsoup提取信息還是很簡單的。只需5或6行代碼即可完成。更多關于Jsoup是如何提取節點信息的,可以參考jsoup的官網教程。下面我們編寫main方法來執行jsoupList方法,看看jsoupList方法是否正確。
public static void main(String[] args) {
String url = "https://voice.hupu.com/nba";
CrawlerBase crawlerBase = new CrawlerBase();
crawlerBase.jsoupList(url);
}
執行main方法,得到如下結果:
從結果中,我們可以看到我們正確提取了我們想要的信息。如果要采集詳情頁的信息,只需要寫一個采集詳情頁的方法,在方法中提取詳情頁對應的節點信息,然后把從列表頁中提取的鏈接傳入到提取詳情頁的方法中頁。
httpclient+正則表達式
上面,我們使用Jsoup正確提取了老虎池列表消息。接下來,我們使用httpclient+正則表達式來提取老虎池列表消息。使用這種方法會涉及哪些問題?httpclient+正則表達式的方式涉及到很多知識點。它涉及到正則表達式、Java正則表達式和httpclient。如果你還不知道,你可以點擊下面的鏈接進行簡單的了解。
正則表達式:正則表達式
Java正則表達式:Java正則表達式
httpclient:httpclient
在pom.xml文件中,我們引入了httpclient相關的Jar包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.10</version>
</dependency>
關于Tiger Pop List的新聞頁面,我們在使用Jsoup模式時做了一個簡單的分析。這里我們不再重復分析。對于正則表達式提取,我們需要找到可以表示列表新聞的結構,例如<div class="list-hd"> <h4> <a href="https://voice.hupu.com/nba/2485508。 html" target="_blank"> 直上天空!魔術官媒曝光富爾茨扣籃炫酷特效圖</a> </h4> </div> 這種結構,每個榜單新聞只有鏈接和標題不同,其余都一樣,而且<div class="list-hd "> 是列出新聞所獨有的。最好不要定期匹配標簽,因為標簽在其他地方也存在,所以我們需要做其他處理來增加我們的難度。
/**
* httpclient + Regular Expressions Get Tiger Pop News List Pages
* @param url Hupu News List page url
*/
public void httpClientList(String url){
try {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpclient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
String body = EntityUtils.toString(entity,"utf-8");
if (body!=null) {
/*
* Replace line breaks, tabs, carriage returns, and remove these symbols. Regular representations are simpler to write.
* Only space symbols and other normal fonts
*/
Pattern p = Pattern.compile("\t|\r|\n");
Matcher m = p.matcher(body);
body = m.replaceAll("");
/*
* Extracting regular expressions from list pages
* li after line breaks are removed
* <div class="list-hd"> <h4> <a target="_blank">Interaction with fans! Celtic Official Sunshine Team Open Training Day Photos </a> </h4> </div>
*/
Pattern pattern = Pattern
.compile("<div class=\"list-hd\">\\s* <h4>\\s* <a href=\"(.*?)\"\\s* target=\"_blank\">(.*?)</a>\\s* </h4>\\s* </div>" );
Matcher matcher = pattern.matcher(body);
// Match all data that conforms to regular expressions
while (matcher.find()){
// String info = matcher.group(0);
// System.out.println(info);
// Extract links and titles
System.out.println("Details page links:"+matcher.group(1)+" ,Details page title:"+matcher.group(2));
}
}else {
System.out.println("Handling failure!!! Get the text empty");
}
} else {
System.out.println("Handling failure!!! Return status code:" + response.getStatusLine().getStatusCode());
}
}catch (Exception e){
e.printStackTrace();
}
}
從代碼行數可以看出,比Jsource模式多很多。雖然代碼很多,但整體來說還是比較簡單的。在上面的方法中,我做了一個特殊的處理。首先,我替換了httpclient獲取到的字符串體中的換行符、制表符和回車符,因為這樣的處理可以減少編寫正則表達式時的一些額外干擾。接下來,我們修改main方法以運行httpClientList方法。
public static void main(String[] args) {
String url = "https://voice.hupu.com/nba";
CrawlerBase crawlerBase = new CrawlerBase();
// crawlerBase.jsoupList(url);
crawlerBase.httpClientList(url);
}
操作結果如下:
使用httpclient+正則表達式,也正確獲取了列表新聞的標題和詳情頁鏈接。這是Java爬蟲系列的第一篇。本文主要介紹Java網絡爬蟲。我們使用jsource和httpclient+定期提取新聞標題和鏈接到Hupu List新聞的詳細頁面。當然還有很多不完整的,比如收集詳情頁信息入數據庫。
以上就是動力節點小編介紹的"Java爬蟲學習,就是這么簡單",希望對大家有幫助,如有疑問,請在線咨詢,有專業老師隨時為您服務。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習