教您使用java爬虫gecco抓取JD全部商品信息(一)
抓取商品列表信息
AllSortPipeline已经将需要进一步抓取的商品列表信息的链接提取出来了,可以看到链接的格式是:http://list.jd.com/list.html?cat=9987,653,659&delivery=1&JL=4_10_0&go=0。因此我们建立商品列表的Bean——ProductList,代码如下:
@Gecco(matchUrl="http://list.jd.com/list.html?cat={cat}&delivery={delivery}&page={page}&JL={JL}&go=0", pipelines={"consolePipeline", "productListPipeline"})
public classProductListimplementsHtmlBean{
private static final long serialVersionUID = 4369792078959596706L;
@Request
private HttpRequest request;
/**
* 抓取列表项的详细内容,包括titile,价格,详情页地址等
*/
@HtmlField(cssPath="#plist .gl-item")
private List<ProductBrief> details;
/**
* 获得商品列表的当前页
*/
@Text
@HtmlField(cssPath="#J_topPage > span > b")
private int currPage;
/**
* 获得商品列表的总页数
*/
@Text
@HtmlField(cssPath="#J_topPage > span > i")
private int totalPage;
public List<ProductBrief> getDetails(){
return details;
}
publicvoidsetDetails(List<ProductBrief> details){
this.details = details;
}
publicintgetCurrPage(){
return currPage;
}
publicvoidsetCurrPage(int currPage){
this.currPage = currPage;
}
publicintgetTotalPage(){
return totalPage;
}
publicvoidsetTotalPage(int totalPage){
this.totalPage = totalPage;
}
public HttpRequest getRequest(){
return request;
}
publicvoidsetRequest(HttpRequest request){
this.request = request;
}
}
currPage和totalPage是页面上的分页信息,为之后的分页抓取提供支持。ProductBrief对象是商品的简介,主要包括标题、预览图、详情页地址等。
public classProductBriefimplementsHtmlBean{
private static final long serialVersionUID = -377053120283382723L;
@Attr("data-sku")
@HtmlField(cssPath=".j-sku-item")
private String code;
@Text
@HtmlField(cssPath=".p-name> a > em")
private String title;
@Image({"data-lazy-img", "src"})
@HtmlField(cssPath=".p-img > a > img")
private String preview;
@Href(click=true)
@HtmlField(cssPath=".p-name > a")
private String detailUrl;
public String getTitle(){
return title;
}
publicvoidsetTitle(String title){
this.title = title;
}
public String getPreview(){
return preview;
}
publicvoidsetPreview(String preview){
this.preview = preview;
}
public String getDetailUrl(){
return detailUrl;
}
publicvoidsetDetailUrl(String detailUrl){
this.detailUrl = detailUrl;
}
public String getCode(){
return code;
}
publicvoidsetCode(String code){
this.code = code;
}
}
这里需要说明一下@Href(click=true)的click属性,click属性形象的说明了,这个链接我们希望gecco继续点击抓取。对于增加了click=true的链接,gecco会自动加入下载队列中,不需要在手动调用SchedulerContext.into()增加。
编写ProductList的业务逻辑
ProductList抓取完成后一般需要进行持久化,也就是将商品的基本信息入库,入库的方式有很多种,这个例子并没有介绍,gecco支持整合spring,可以利用spring进行pipeline的开发,大家可以参考gecco-spring这个项目。本例子是进行了控制台输出。ProductList的业务处理还有一个很重要的任务,就是对分页的处理,列表页通常都有很多页,如果需要全部抓取,我们需要将下一页的链接入抓取队列。
@PipelineName("productListPipeline")
public classProductListPipelineimplementsPipeline<ProductList> {
@Override
publicvoidprocess(ProductList productList){
HttpRequest currRequest = productList.getRequest();
//下一页继续抓取
int currPage = productList.getCurrPage();
int nextPage = currPage + 1;
int totalPage = productList.getTotalPage();
if(nextPage <= totalPage) {
String nextUrl = "";
String currUrl = currRequest.getUrl();
if(currUrl.indexOf("page=") != -1) {
nextUrl = StringUtils.replaceOnce(currUrl, "page=" + currPage, "page=" + nextPage);
} else {
nextUrl = currUrl + "&" + "page=" + nextPage;
}
SchedulerContext.into(currRequest.subRequest(nextUrl));
}
}
}
JD的列表页通过page参数来指定页码,我们通过替换page参数达到分页抓取的目的。至此,所有的商品的列表信息都已经可以正常抓取了。
相关推荐
}demo地址:教您使用java爬虫gecco抓取JD全部商品信息(一)教您使用java爬虫gecco抓取JD全部商品信息(二)教您使用java爬虫gecco抓取JD全部商品信息(三)集成Htmlunit下载页面爬虫的监控一个完整的例子,分页处理...
对java爬虫的讲解以及对工具框架的应用 对java爬虫的讲解以及对工具框架的应用
Java爬虫信息抓取.pdf
java爬虫抓取网页数据教程.pdf
Gecco是一款用java语言开发的轻量化的易用的网络爬虫。Gecco整合了jsoup、httpclient、fastjson、spring、htmlunit、redission等优秀框架,让您只需要配置一些jquery风格的选择器就能很快的写出一个爬虫。Gecco框架...
本篇文章主要介绍了JAVA 爬虫Gecco工具抓取新闻实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
java爬虫抓取网页数据教程(20210809124656).pdf
java爬虫,可以抓取网页中图片信息,适用与爬虫学习者
Java爬虫信息抓取共14页.pdf.zip
Java爬虫,信息抓取的实现 详细完整源码实例打包给大家,需要的可以下载下载学习!!!
java爬虫抓取城市数据,数据抓取后直接持久化到数据库当中。博客地址http://blog.csdn.net/qq_23994787/article/details/78107890 如果没有积分的话,在主页给博主发邮件获取。
简单Java爬虫,抓取网页内容,让你了解爬虫机制,熟悉网络编程
java爬虫抓取图片,单线程,代码简洁,适合新手使用,有问题的话可以来博主主页留言探讨,一起解决技术问题。
本项目是基于Java开发的网络爬虫Gecco设计源码,主要使用Java进行开发。项目共包含164个文件,其中Java源代码文件153个,Git忽略配置文件2个,Markdown文档文件2个,YAML配置文件1个,项目许可证文件1个,JPG图片...
通过Java网络爬虫爬取指定代理ip网上的ip,利用了jsoup、httpclient技术实现
利用HttpClient抓取网页中简单的数据,数据可存入数据库进行分析
【Java爬虫】信息抓取的实现 完整实例(源码)
java爬虫抓取新闻.zip
java爬虫需要的jar包。httpclient,jsoup等等。直接导入项目使用 java爬虫需要的jar包。httpclient,jsoup等等。直接导入项目使用 java爬虫需要的jar包。httpclient,jsoup等等。直接导入项目使用java爬虫需要的jar包...
使用java代码基于MyEclipse开发环境实现爬虫抓取网页中的表格数据,将抓取到的数据在控制台打印出来,需要后续处理的话可以在打印的地方对数据进行操作。包解压后导入MyEclipse就可以使用,在TestCrawTable中右键...