java爬蟲Gecco工具抓取新聞實(shí)例
最近看到Gecoo爬蟲工具,感覺(jué)比較簡(jiǎn)單好用,所有寫個(gè)DEMO測(cè)試一下,抓取網(wǎng)站
http://zj.zjol.com.cn/home.html,主要抓取新聞的標(biāo)題和發(fā)布時(shí)間做為抓取測(cè)試對(duì)象。抓取HTML節(jié)點(diǎn)通過(guò)像Jquery選擇器一樣選擇節(jié)點(diǎn),非常方便,Gecco代碼主要利用注解實(shí)現(xiàn)來(lái)實(shí)現(xiàn)URL匹配,看起來(lái)比較簡(jiǎn)潔美觀。
添加Maven依賴
<dependency> <groupId>com.geccocrawler</groupId> <artifactId>gecco</artifactId> <version>1.0.8</version> </dependency>
編寫抓取列表頁(yè)面
@Gecco(matchUrl = "http://zj.zjol.com.cn/home.html?pageIndex={pageIndex}&pageSize={pageSize}",pipelines = "zJNewsListPipelines")
public class ZJNewsGeccoList implements HtmlBean {
@Request
private HttpRequest request;
@RequestParameter
private int pageIndex;
@RequestParameter
private int pageSize;
@HtmlField(cssPath = "#content > div > div > div.con_index > div.r.main_mod > div > ul > li > dl > dt > a")
private List<HrefBean> newList;
}
@PipelineName("zJNewsListPipelines")
public class ZJNewsListPipelines implements Pipeline<ZJNewsGeccoList> {
public void process(ZJNewsGeccoList zjNewsGeccoList) {
HttpRequest request=zjNewsGeccoList.getRequest();
for (HrefBean bean:zjNewsGeccoList.getNewList()){
//進(jìn)入祥情頁(yè)面抓取
SchedulerContext.into(request.subRequest("http://zj.zjol.com.cn"+bean.getUrl()));
}
int page=zjNewsGeccoList.getPageIndex()+1;
String nextUrl = "http://zj.zjol.com.cn/home.html?pageIndex="+page+"&pageSize=100";
//抓取下一頁(yè)
SchedulerContext.into(request.subRequest(nextUrl));
}
}
編寫抓取祥情頁(yè)面
@Gecco(matchUrl = "http://zj.zjol.com.cn/news/[code].html" ,pipelines = "zjNewsDetailPipeline")
public class ZJNewsDetail implements HtmlBean {
@Text
@HtmlField(cssPath = "#headline")
private String title ;
@Text
@HtmlField(cssPath = "#content > div > div.news_con > div.news-content > div:nth-child(1) > div > p.go-left.post-time.c-gray")
private String createTime;
}
@PipelineName("zjNewsDetailPipeline")
public class ZJNewsDetailPipeline implements Pipeline<ZJNewsDetail> {
public void process(ZJNewsDetail zjNewsDetail) {
System.out.println(zjNewsDetail.getTitle()+" "+zjNewsDetail.getCreateTime());
}
}
啟動(dòng)主函數(shù)
public class Main {
public static void main(String [] rags){
GeccoEngine.create()
//工程的包路徑
.classpath("com.zhaochao.gecco.zj")
//開始抓取的頁(yè)面地址
.start("http://zj.zjol.com.cn/home.html?pageIndex=1&pageSize=100")
//開啟幾個(gè)爬蟲線程
.thread(10)
//單個(gè)爬蟲每次抓取完一個(gè)請(qǐng)求后的間隔時(shí)間
.interval(10)
//使用pc端userAgent
.mobile(false)
//開始運(yùn)行
.run();
}
}
抓取結(jié)果


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)戰(zhàn)之用hutool-db實(shí)現(xiàn)多數(shù)據(jù)源配置
在微服務(wù)搭建中經(jīng)常會(huì)使用到多數(shù)據(jù)庫(kù)情形這個(gè)時(shí)候,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)戰(zhàn)之用hutool-db實(shí)現(xiàn)多數(shù)據(jù)源配置的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
selenium-java實(shí)現(xiàn)自動(dòng)登錄跳轉(zhuǎn)頁(yè)面方式
利用Selenium和Java語(yǔ)言可以編寫一個(gè)腳本自動(dòng)刷新網(wǎng)頁(yè),首先,需要確保Google瀏覽器和Chrome-Driver驅(qū)動(dòng)的版本一致,通過(guò)指定網(wǎng)站下載對(duì)應(yīng)版本的瀏覽器和驅(qū)動(dòng),在Maven項(xiàng)目中添加依賴,編寫腳本實(shí)現(xiàn)網(wǎng)頁(yè)的自動(dòng)刷新,此方法適用于需要頻繁刷新網(wǎng)頁(yè)的場(chǎng)景,簡(jiǎn)化了操作,提高了效率2024-11-11
SpringBoot動(dòng)態(tài)生成接口實(shí)現(xiàn)流程示例講解
最近遇到一個(gè)需求,需要在程序運(yùn)行過(guò)程中,可以動(dòng)態(tài)新增接口,自定義接口參數(shù)名稱,基本類型,以及請(qǐng)求方法,請(qǐng)求頭等等。通過(guò)幾天的研究,找到了我需要的解決方案2023-01-01
Java8流式API將實(shí)體類列表轉(zhuǎn)換為視圖對(duì)象列表的示例
這篇文章主要介紹了Java8流式API將實(shí)體類列表轉(zhuǎn)換為視圖對(duì)象列表的示例,文中有相關(guān)的代碼示例供大家參考,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-11-11
Springboot配置security basic path無(wú)效解決方案
這篇文章主要介紹了Springboot配置security basic path無(wú)效解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
SpringBoot+Elasticsearch實(shí)現(xiàn)數(shù)據(jù)搜索的方法詳解
Elasticsearch是一個(gè)基于Lucene的搜索服務(wù)器。它提供了一個(gè)分布式多用戶能力的全文搜索引擎,基于RESTful?web接口。本文將利用SpringBoot整合Elasticsearch實(shí)現(xiàn)海量級(jí)數(shù)據(jù)搜索,需要的可以參考一下2022-05-05
Java實(shí)現(xiàn)將漢字轉(zhuǎn)化為漢語(yǔ)拼音的方法
這篇文章主要介紹了Java實(shí)現(xiàn)將漢字轉(zhuǎn)化為漢語(yǔ)拼音的方法,實(shí)例演示了Java引用pinyin4j庫(kù)實(shí)現(xiàn)漢子轉(zhuǎn)化成拼音的使用技巧,需要的朋友可以參考下2015-12-12

