gecco爬虫框架是几年前的一个框架了,但是现阶段用来爬一些普通网站使用起来还是很方便的。

pom引入:

<dependency><groupId>com.geccocrawler</groupId><artifactId>gecco</artifactId><version>1.3.21</version>
</dependency>

代码可分为三部分:

一、启动代码

HttpGetRequest start = new HttpGetRequest("https://www.xxxx.com/aa/bb/493645.shtml");
start.setCharset("UTF-8");
GeccoEngine.create().classpath("com.lujing.mydemo")//开始抓取的页面地址.start(start)//开启几个爬虫线程.thread(2)//单个爬虫每次抓取完一个请求后的间隔时间.interval(100).run();

二、HtmlBean 实现类

如:

import com.geccocrawler.gecco.annotation.*;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.spider.HrefBean;
import com.geccocrawler.gecco.spider.HtmlBean;import java.util.List;/*** 抓取京东的某个商品列表页* * @author memory**/
@Gecco(matchUrl="https://www.xxxx.com/aa/bb/index_{page}.shtml", pipelines={"consolePipeline", "myProductListPipeline"})
public class ProductList implements HtmlBean {private static final long serialVersionUID = 4369792078959596706L;@Requestprivate HttpRequest request;@RequestParameterprivate int page;/*** 抓取列表项的详细内容,包括titile,价格,详情页地址等*/@HtmlField(cssPath="#top > div.wrap > div > div.content > div.container > div.content_main > div.column_list > ul > li > div.left_con > a")private List<HrefBean> details;@JSVar(var = "gioParam",jsonpath = "$.channel_en")private String gioParam;@Html@HtmlField(cssPath="html")private String allHtml;@Image@HtmlField(cssPath="#top > div.wrap > div > div.content > div.container > div.content_main > div.column_list > ul > li > div.img_con > img")private List<String> images;/*** 获得商品列表的总页数*/@Text@HtmlField(cssPath="#top > div.wrap > div > div.content > div.container > div.content_main > div:nth-child(2) > div > ul > li:nth-last-child(2) > a")private int totalPage;public String getGioParam() {return gioParam;}public void setGioParam(String gioParam) {this.gioParam = gioParam;}public void setRequest(HttpRequest request) {this.request = request;}public void setPage(int page) {this.page = page;}public void setDetails(List<HrefBean> details) {this.details = details;}public String getAllHtml() {return allHtml;}public void setAllHtml(String allHtml) {this.allHtml = allHtml;}public void setTotalPage(int totalPage) {this.totalPage = totalPage;}public HttpRequest getRequest() {return request;}public int getPage() {return page;}public List<HrefBean> getDetails() {return details;}public int getTotalPage() {return totalPage;}public List<String> getImages() {return images;}public void setImages(List<String> images) {this.images = images;}
}

该实现类的作用即为匹配爬取的url进行处理。如启动类第一个url,适配获取html中的页码及想要的信息等。

三、管道类Pipeline实现类

该类即处理第二步中获取的详细信息的类,可以翻页继续爬取等。

如:

import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.scheduler.SchedulerContext;
import com.geccocrawler.gecco.spider.HrefBean;
import com.huadongfeng.project.utils.FileUtil;import java.util.List;@PipelineName("myProductListPipeline")
public class ProductListPipeline implements Pipeline<ProductList> {@Overridepublic void process(ProductList productList) {HttpRequest currRequest = productList.getRequest();int page = productList.getPage();int totalPage = productList.getTotalPage();System.out.println(totalPage);if(page<4){int i = page + 1;String url = "https://www.xxx.com/aa/bb/index_" + i + ".shtml";System.out.println(url);SchedulerContext.into(currRequest.subRequest(url));}String gioParam = productList.getGioParam();System.out.println(gioParam);String allHtml = productList.getAllHtml();List<String> images = productList.getImages();for (String img : images){System.out.println("img循环详情页"+img);//       DownloadImage.download("E:\\img",img);}List<HrefBean> details = productList.getDetails();for (HrefBean bean:details){
//       System.out.println("列表循环详情页"+bean.getTitle());//进入祥情页面抓取
//       SchedulerContext.into(currRequest.subRequest(bean.getUrl()));FileUtil.writeFile("进入详情页=="+bean.getUrl()+ System.lineSeparator(),"E:\\","answer.txt","UTF-8");}}}

gecco爬虫框架使用指南相关推荐

  1. Java 通过gecco快速搭建一个爬虫框架

    Java gecco 爬虫Demo Gecco是一款用java语言开发的轻量化的易用的网络爬虫框架. 官网: http://www.geccocrawler.com/ 1.导入依赖 <depen ...

  2. java分布式爬虫_Java分布式爬虫框架:Gecco 入门

    什么是Gecco ? Gecco 是一款用java语言开发的轻量化的易用的网络爬虫,整合了jsoup.httpclient.fastjson.spring.htmlunit.redission等优秀框 ...

  3. 把玩爬虫框架Gecco

    如果你现在接到一个任务,获取某某行业下的分类. 作为一个非该领域专家,没有深厚的运营经验功底,要提供一套摆的上台面且让人信服的行业分类,恐怕不那么简单. 找不到专家没有关系,我们可以爬虫.把那些专家的 ...

  4. Java爬虫框架wemgic_Java爬虫框架-WebMagic挖坑补坑指南

    以前总是用的Python下的Scrapy和crawley和PHP的小众爬虫框架,最近突然想到了何不用下Java下的框架试试? 查了下Java下的爬虫框架自然也不在少数,Nutch,WebMagic,W ...

  5. Python 爬虫框架 - PySpider

    Python爬虫进阶四之PySpider的用法:http://cuiqingcai.com/2652.html 网络爬虫剖析,以Pyspider为例:http://python.jobbole.com ...

  6. Java爬虫框架调研

    Python中大的爬虫框架有scrapy(风格类似django),pyspider(国产python爬虫框架). 除了Python,Java中也有许多爬虫框架. nutch apache下的开源爬虫程 ...

  7. 什么是爬虫,常见的java爬虫框架有哪些?-蛙课网

    随着互联网的发展,编程程序语言也开始被越来越多的人所掌握,与此同时,java语言是使用范围最广的编程语言.今天我们一起了解一下什么是爬虫,java爬虫框架有哪些. 网络爬虫(又称为网页蜘蛛,网络机器人 ...

  8. 使用WebCollector爬虫框架进行微信公众号文章爬取并持久化

    〇.Java爬虫框架有哪些? 1.nutch:Apache下开源爬虫项目,适合做搜索引擎,分布式爬虫只是其中一个功能,功能丰富,文档完整. 2.heritrix:比较成熟,用的人较多,有自己的web管 ...

  9. gecco爬虫初见与综述

    2021SC@SDUSC Gecco是一款用java语言开发的轻量化的易用的网络爬虫.作为国内大佬研发的java网络爬虫,Gecco整合了jsoup.httpclient.fastjson.sprin ...

最新文章

  1. MobileIMSDK怎样修改Server端和安卓端TCP连接方式时报文的的限制大小
  2. c语言a 寻路算法,JS/HTML5游戏常用算法之路径搜索算法 A*寻路算法完整实例
  3. bash 命令提示符_命令行上每天的Bash提示
  4. 错误: 找不到或无法加载主类
  5. ActiveReports 报表应用教程 (9)---交互式报表之动态排序
  6. 一句代码搞定权限请求,从未如此简单
  7. MySQL5.7导出数据表
  8. webgl存本地文件_Unity发布WebGL后加载本地文件
  9. 华为发布会2019鸿蒙,鸿蒙,来了!华为正式发布鸿蒙OS操作系统
  10. 主机驱动与外设驱动的分离思想
  11. 一步一步教你Win8专业版+U盘安装+kms激活
  12. 字符设备驱动应用---LED设备驱动实现
  13. “IP地址/24”是什么意思
  14. 2020总结 2021规划
  15. vue-cli3 跑项目时 ‘98%’ after emitting CopyPlugin
  16. 计算机基础知识学习第七课,7、新建文件夹--电脑基础知识
  17. acm刷题一些总结,至每一个努力拼搏的acmer
  18. 关于数据库的根本目的之一——数据独立性
  19. jsp+ssm二手书图书回收捐赠管理系统springboot
  20. 【人工智能】人工智能顶级研究所+人工智能顶级研究机构+人工智能顶级大学推荐!

热门文章

  1. Unity3d 分辨率设置无效
  2. JVM的内存区域和垃圾回收机制
  3. 论文阅读笔记:Neural Speech Synthesis with Transformer Network
  4. 【待整合】Oracle数据库 1 - 版本、安装、目录、网络监听配置、创建用户
  5. jshint和jslint的区别
  6. php上传头像的代码,php实现文件上传及头像预览功能
  7. 空气净化器全国产化电子元件推荐方案
  8. 视频教程-Python开发零基础入门-计算机基础-Python
  9. 磨刀不误砍柴工,带你搞定云网络系统性能测试
  10. Codeforces 825 D Suitable Replacement