更新時(shí)間:2020-03-11 10:55:40 來源:動(dòng)力節(jié)點(diǎn) 瀏覽3322次
Jsoup方式提取信息
我們先來使用Jsoup的方式提取新聞信息,如果你還不知道Jsoup,請參考https://jsoup.org/
先建立一個(gè)Springboot項(xiàng)目,名字就隨意啦,在pom.xml中引入Jsoup的依賴
<dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.12.1</version> </dependency>
好了,接下來我們一起分析頁面吧,想必你還沒瀏覽過吧,點(diǎn)擊這里瀏覽虎撲新聞。在列表頁中,我們利用F12審查元素查看頁面結(jié)構(gòu),經(jīng)過我們分析發(fā)現(xiàn)列表新聞在<divclass="news-list">標(biāo)簽下,每一條新聞都是一個(gè)li標(biāo)簽,分析結(jié)果如下圖所示:

由于我們前面已經(jīng)知道了css選擇器,我們結(jié)合瀏覽器的Copy功能,編寫出我們a標(biāo)簽的css選擇器代碼:div.news-list>ul>li>div.list-hd>h4>a,一切都準(zhǔn)備好了,我們一起來編寫Jsoup方式提取信息的代碼:
/**
*jsoup方式獲取虎撲新聞列表頁
*@paramurl虎撲新聞列表頁url
*/
publicvoidjsoupList(Stringurl){
try{
Documentdocument=Jsoup.connect(url).get();
//使用css選擇器提取列表新聞a標(biāo)簽
//<ahref="https://voice.hupu.com/nba/2484553.html"target="_blank">霍華德:夏休期內(nèi)曾節(jié)食30天,這考驗(yàn)了我的身心</a>
Elementselements=document.select("div.news-list>ul>li>div.list-hd>h4>a");
for(Elementelement:elements){
//System.out.println(element);
//獲取詳情頁鏈接
Stringd_url=element.attr("href");
//獲取標(biāo)題
Stringtitle=element.ownText();
System.out.println("詳情頁鏈接:"+d_url+",詳情頁標(biāo)題:"+title);
}
}catch(IOExceptione){
e.printStackTrace();
}
}使用Jsoup方式提取還是非常簡單的,就5、6行代碼就完成了,關(guān)于更多Jsoup如何提取節(jié)點(diǎn)信息的方法可以參考jsoup的官網(wǎng)教程。我們編寫main方法,來執(zhí)行jsoupList方法,看看jsoupList方法是否正確。
publicstaticvoidmain(String[]args){
Stringurl="https://voice.hupu.com/nba";
CrawlerBasecrawlerBase=newCrawlerBase();
crawlerBase.jsoupList(url);
}執(zhí)行main方法,得到如下結(jié)果:

從結(jié)果中可以看出,我們已經(jīng)正確的提取到了我們想要的信息,如果你想采集詳情頁的信息,只需要編寫一個(gè)采集詳情頁的方法,在方法中提取詳情頁相應(yīng)的節(jié)點(diǎn)信息,然后將列表頁提取的鏈接傳入提取詳情頁方法即可。
httpclient+正則表達(dá)式
上面我們使用了Jsoup方式正確提取了虎撲列表新聞,接下來我們使用httpclient+正則表達(dá)式的方式來提取,看看使用這種方式又會(huì)涉及到哪些問題?httpclient+正則表達(dá)式的方式涉及的知識(shí)點(diǎn)還是蠻多的,它涉及到了正則表達(dá)式、Java正則表達(dá)式、httpclient。如果你還不知道這些知識(shí),可以點(diǎn)擊下方鏈接簡單了解一下:
正則表達(dá)式:正則表達(dá)式
Java正則表達(dá)式:Java正則表達(dá)式
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>
/**
*httpclient+正則表達(dá)式獲取虎撲新聞列表頁
*@paramurl虎撲新聞列表頁url
*/
publicvoidhttpClientList(Stringurl){
try{
CloseableHttpClienthttpclient=HttpClients.createDefault();
HttpGethttpGet=newHttpGet(url);
CloseableHttpResponseresponse=httpclient.execute(httpGet);
if(response.getStatusLine().getStatusCode()==200){
HttpEntityentity=response.getEntity();
Stringbody=EntityUtils.toString(entity,"utf-8");
if(body!=null){
/*
*替換掉換行符、制表符、回車符,去掉這些符號(hào),正則表示寫起來更簡單一些
*只有空格符號(hào)和其他正常字體
*/
Patternp=Pattern.compile("\t|\r|\n");
Matcherm=p.matcher(body);
body=m.replaceAll("");
/*
*提取列表頁的正則表達(dá)式
*去除換行符之后的li
*<divclass="list-hd"><h4><ahref="https://voice.hupu.com/nba/2485167.html"target="_blank">與球迷親切互動(dòng)!凱爾特人官方曬球隊(duì)開放訓(xùn)練日照片</a></h4></div>
*/
Patternpattern=Pattern
.compile("<divclass=\"list-hd\">\\s*<h4>\\s*<ahref=\"(.*?)\"\\s*target=\"_blank\">(.*?)</a>\\s*</h4>\\s*</div>");
Matchermatcher=pattern.matcher(body);
//匹配出所有符合正則表達(dá)式的數(shù)據(jù)
while(matcher.find()){
//Stringinfo=matcher.group(0);
//System.out.println(info);
//提取出鏈接和標(biāo)題
System.out.println("詳情頁鏈接:"+matcher.group(1)+",詳情頁標(biāo)題:"+matcher.group(2));
}
}else{
System.out.println("處理失?。。。~@取正文內(nèi)容為空");
}
}else{
System.out.println("處理失?。。。》祷貭顟B(tài)碼:"+response.getStatusLine().getStatusCode());
}
}catch(Exceptione){
e.printStackTrace();
}
}從代碼的行數(shù)可以看出,比Jsoup方式要多不少,代碼雖然多,但是整體來說比較簡單,在上面方法中我做了一段特殊處理,我先替換了httpclient獲取的字符串body中的換行符、制表符、回車符,因?yàn)檫@樣處理,在編寫正則表達(dá)式的時(shí)候能夠減少一些額外的干擾。接下來我們修改main方法,運(yùn)行httpClientList方法。
publicstaticvoidmain(String[]args){
Stringurl="https://voice.hupu.com/nba";
CrawlerBasecrawlerBase=newCrawlerBase();
//crawlerBase.jsoupList(url);
crawlerBase.httpClientList(url);
}
使用httpclient+正則表達(dá)式的方式同樣正確的獲取到了列表新聞的標(biāo)題和詳情頁鏈接。到此Java爬蟲系列博文第一篇就寫完了,這一篇主要是Java網(wǎng)絡(luò)爬蟲的入門,我們使用了jsoup和httpclient+正則的方式提取了虎撲列表新聞的新聞標(biāo)題和詳情頁鏈接。當(dāng)然這里還有很多沒有完成,比如采集詳情頁信息存入數(shù)據(jù)庫等。
以上就是動(dòng)力節(jié)點(diǎn)Java培訓(xùn)機(jī)構(gòu)小編介紹的“Java爬蟲學(xué)習(xí):網(wǎng)絡(luò)爬蟲”的內(nèi)容,希望對大家有幫助,如有疑問,請?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為你服務(wù)。
相關(guān)閱讀