gpt4 book ai didi

java爬虫Gecco工具抓取新闻实例

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章java爬虫Gecco工具抓取新闻实例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

最近看到Gecoo爬虫工具,感觉比较简单好用,所有写个DEMO测试一下,抓取网站 http://zj.zjol.com.cn/home.html,主要抓取新闻的标题和发布时间做为抓取测试对象。抓取HTML节点通过像Jquery选择器一样选择节点,非常方便,Gecco代码主要利用注解实现来实现URL匹配,看起来比较简洁美观.

添加Maven依赖 。

?
1
2
3
4
5
< dependency >
    < groupId >com.geccocrawler</ groupId >
    < artifactId >gecco</ artifactId >
    < version >1.0.8</ version >
</ dependency >

编写抓取列表页面 。

?
1
2
3
4
5
6
7
8
9
10
11
@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;
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@PipelineName ( "zJNewsListPipelines" )
public class ZJNewsListPipelines implements Pipeline<ZJNewsGeccoList> {
   public void process(ZJNewsGeccoList zjNewsGeccoList) {
     HttpRequest request=zjNewsGeccoList.getRequest();
     for (HrefBean bean:zjNewsGeccoList.getNewList()){
       //进入祥情页面抓取
     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" ;
     //抓取下一页
     SchedulerContext.into(request.subRequest(nextUrl));
   }
}

编写抓取祥情页面 。

?
1
2
3
4
5
6
7
8
9
10
11
@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;
}
?
1
2
3
4
5
6
@PipelineName ( "zjNewsDetailPipeline" )
public class ZJNewsDetailPipeline implements Pipeline<ZJNewsDetail> {
   public void process(ZJNewsDetail zjNewsDetail) {
     System.out.println(zjNewsDetail.getTitle()+ " " +zjNewsDetail.getCreateTime());
   }
}

启动主函数 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Main {
   public static void main(String [] rags){
     GeccoEngine.create()
         //工程的包路径
         .classpath( "com.zhaochao.gecco.zj" )
         //开始抓取的页面地址
         .start( "http://zj.zjol.com.cn/home.html?pageIndex=1&pageSize=100" )
         //开启几个爬虫线程
         .thread( 10 )
         //单个爬虫每次抓取完一个请求后的间隔时间
         .interval( 10 )
         //使用pc端userAgent
         .mobile( false )
         //开始运行
         .run();
   }
}

抓取结果 。

java爬虫Gecco工具抓取新闻实例

java爬虫Gecco工具抓取新闻实例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.

最后此篇关于java爬虫Gecco工具抓取新闻实例的文章就讲到这里了,如果你想了解更多关于java爬虫Gecco工具抓取新闻实例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com