今天我們就來說說關(guān)于Java網(wǎng)絡(luò)爬蟲的介紹。在本文中,我們以虎撲榜的新聞標題和詳情頁為例。我們需要提取的內(nèi)容如下:
我們需要提取帶圓圈的文本及其對應(yīng)的鏈接。在提取的過程中,我們會使用兩種方式提取,一種是Jsoup,一種是httpclient+正則表達式。這也是Java網(wǎng)絡(luò)爬蟲常用的兩種方式。你不知道這兩種方式是無關(guān)緊要的。后面會有相應(yīng)的手冊。在正式編寫提取程序之前,先講解一下Java爬蟲系列博文的環(huán)境。本系列博文中的所有demo都是使用SpringBoot搭建的。無論您使用什么環(huán)境,您只需要正確導(dǎo)入相應(yīng)的包即可。
基于Jsoup的信息抽取
首先創(chuàng)建一個隨機名稱的Springboot項目,并在pom.xml中引入Jsoup依賴
<dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.12.1</version></dependency>
好的,我們一起來分析一下頁面。你還沒有瀏覽它。在列表頁面中,我們使用 F12 評論元素來查看頁面結(jié)構(gòu)。經(jīng)過我們的分析,我們發(fā)現(xiàn)列表新聞在<div class="news-list">標簽下,每條新聞都是一個li標簽。分析結(jié)果如下:
因為之前我們已經(jīng)知道了css選擇器,所以我們編譯了我們a標簽的css選擇器的代碼:Div。新聞列表 > UL > Li > Div. list-hd > H4 > a,結(jié)合瀏覽器的opy功能。全部都準備好了。我們一起編譯了Jsoup模式提取信息的代碼:
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行代碼即可完成。更多關(guān)于Jsoup是如何提取節(jié)點信息的,可以參考jsoup的官網(wǎng)教程。下面我們編寫main方法來執(zhí)行jsoupList方法,看看jsoupList方法是否正確。
public static void main(String[] args) { String url = "https://voice.hupu.com/nba"; Crawlerbase crawlerbase = new Crawlerbase(); crawlerbase.jsoupList(url);}
執(zhí)行main方法,得到如下結(jié)果:
從結(jié)果中,我們可以看到我們正確提取了我們想要的信息。如果要采集詳情頁的信息,只需要寫一個采集詳情頁的方法,在方法中提取詳情頁對應(yīng)的節(jié)點信息,然后把從列表頁中提取的鏈接傳入到提取詳情頁的方法中頁。
httpclient+正則表達式
上面,我們使用Jsoup正確提取了老虎池列表消息。接下來,我們使用httpclient+正則表達式來提取老虎池列表消息。使用這種方法會涉及哪些問題?httpclient+正則表達式的方式涉及到很多知識點。它涉及到正則表達式、Java正則表達式和httpclient。如果你還不知道,你可以點擊下面的鏈接進行簡單的了解。
正則表達式:正則表達式
Java正則表達式:Java正則表達式
httpclient:httpclient
在pom.xml文件中,我們引入了httpclient相關(guān)的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>
關(guān)于Tiger Pop List的新聞頁面,我們在使用Jsoup模式時做了一個簡單的分析。這里我們不再重復(fù)分析。對于正則表達式提取,我們需要找到可以表示列表新聞的結(jié)構(gòu),例如<div class="list-hd"> <h4> <a href="https://voice.hupu.com/nba/2485508。 html" target="_blank"> 直上天空!魔術(shù)官媒曝光富爾茨扣籃炫酷特效圖</a> </h4> </div> 這種結(jié)構(gòu),每個榜單新聞只有鏈接和標題不同,其余都一樣,而且<div class="list-hd "> 是列出新聞所獨有的。最好不要定期匹配標簽,因為標簽在其他地方也存在,所以我們需要做其他處理來增加我們的難度。
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) { Pattern p = Pattern.compile("t|r|n"); Matcher m = p.matcher(body); body = m.replaceAll(""); 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(); }}
從代碼行數(shù)可以看出,比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);}
操作結(jié)果如下:
使用httpclient+正則表達式,也正確獲取了列表新聞的標題和詳情頁鏈接。這是Java爬蟲系列的第一篇。本文主要介紹Java網(wǎng)絡(luò)爬蟲。我們使用jsource和httpclient+定期提取新聞標題和鏈接到Hupu List新聞的詳細頁面。當然還有很多不完整的,比如收集詳情頁信息入數(shù)據(jù)庫。
以上就是長沙牛耳教育小編介紹的"Java爬蟲學(xué)習(xí),就是這么簡單",希望對大家有幫助,如有疑問,請在線咨詢,有專業(yè)老師隨時為您服務(wù)。
Java學(xué)習(xí)