教您使用java爬虫gecco抓取JD全部商品信息(一)
gecco爬虫
如果对gecco还没有了解可以参看一下gecco的github首页。gecco爬虫十分的简单易用,JD全部商品信息的抓取9个类就能搞定。
JD网站的分析
要抓取JD网站的全部商品信息,我们要先分析一下网站,京东网站可以大体分为三级,首页上通过分类跳转到商品列表页,商品列表页对每个商品有详情页。那么我们通过找到所有分类就能逐个分类抓取商品信息。
入口地址
http://www.jd.com/allSort.aspx,这个地址是JD全部商品的分类列表,我们以该页面作为开始页面,抓取JD的全部商品信息
新建开始页面的HtmlBean类AllSort
@Gecco(matchUrl="http://www.jd.com/allSort.aspx", pipelines={"consolePipeline", "allSortPipeline"})
public classAllSortimplementsHtmlBean{
private static final long serialVersionUID = 665662335318691818L;
@Request
private HttpRequest request;
//手机
@HtmlField(cssPath=".category-items > div:nth-child(1) > div:nth-child(2) > div.mc > div.items > dl")
private List<Category> mobile;
//家用电器
@HtmlField(cssPath=".category-items > div:nth-child(1) > div:nth-child(3) > div.mc > div.items > dl")
private List<Category> domestic;
public List<Category> getMobile(){
return mobile;
}
publicvoidsetMobile(List<Category> mobile){
this.mobile = mobile;
}
public List<Category> getDomestic(){
return domestic;
}
publicvoidsetDomestic(List<Category> domestic){
this.domestic = domestic;
}
public HttpRequest getRequest(){
return request;
}
publicvoidsetRequest(HttpRequest request){
this.request = request;
}
}
可以看到,这里以抓取手机和家用电器两个大类的商品信息为例,可以看到每个大类都包含若干个子分类,用List<Category>表示。gecco支持Bean的嵌套,可以很好的表达html页面结构。Category表示子分类信息内容,HrefBean是共用的链接Bean。
public classCategoryimplementsHtmlBean{
private static final long serialVersionUID = 3018760488621382659L;
@Text
@HtmlField(cssPath="dt a")
private String parentName;
@HtmlField(cssPath="dd a")
private List<HrefBean> categorys;
public String getParentName(){
return parentName;
}
publicvoidsetParentName(String parentName){
this.parentName = parentName;
}
public List<HrefBean> getCategorys(){
return categorys;
}
publicvoidsetCategorys(List<HrefBean> categorys){
this.categorys = categorys;
}
}
获取页面元素cssPath的小技巧
上面两个类难点就在cssPath的获取上,这里介绍一些cssPath获取的小技巧。用Chrome浏览器打开需要抓取的网页,按F12进入发者模式。选择你要获取的元素,如图:
在浏览器右侧选中该元素,鼠标右键选择Copy--Copy selector,即可获得该元素的cssPath
body > div:nth-child(5) > div.main-classify > div.list > div.category-items.clearfix > div:nth-child(1) > div:nth-child(2) > div.mc > div.items
如果你对jquery的selector有了解,另外我们只希望获得dl元素,因此即可简化为:
.category-items > div:nth-child(1) > div:nth-child(2) > div.mc > div.items > dl
编写AllSort的业务处理类
完成对AllSort的注入后,我们需要对AllSort进行业务处理,这里我们不做分类信息持久化等处理,只对分类链接进行提取,进一步抓取商品列表信息。看代码:
@PipelineName("allSortPipeline")
public classAllSortPipelineimplementsPipeline<AllSort> {
@Override
public void process(AllSort allSort) {
List<Category> categorys = allSort.getMobile();
for(Category category : categorys) {
List<HrefBean> hrefs = category.getCategorys();
for(HrefBean href : hrefs) {
String url = href.getUrl()+"&delivery=1&page=1&JL=4_10_0&go=0";
HttpRequest currRequest = allSort.getRequest();
SchedulerContext.into(currRequest.subRequest(url));
}
}
}
}
@PipelinName定义该pipeline的名称,在AllSort的@Gecco注解里进行关联,这样,gecco在抓取完并注入Bean后就会逐个调用@Gecco定义的pipeline了。为每个子链接增加"&delivery=1&page=1&JL=4_10_0&go=0"的目的是只抓取京东自营并且有货的商品。SchedulerContext.into()方法是将待抓取的链接放入队列中等待进一步抓取。
相关推荐
}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开发的网络爬虫Gecco设计源码,主要使用Java进行开发。项目共包含164个文件,其中Java源代码文件153个,Git忽略配置文件2个,Markdown文档文件2个,YAML配置文件1个,项目许可证文件1个,JPG图片...
java爬虫抓取城市数据,数据抓取后直接持久化到数据库当中。博客地址http://blog.csdn.net/qq_23994787/article/details/78107890 如果没有积分的话,在主页给博主发邮件获取。
简单Java爬虫,抓取网页内容,让你了解爬虫机制,熟悉网络编程
java爬虫抓取图片,单线程,代码简洁,适合新手使用,有问题的话可以来博主主页留言探讨,一起解决技术问题。
通过Java网络爬虫爬取指定代理ip网上的ip,利用了jsoup、httpclient技术实现
利用HttpClient抓取网页中简单的数据,数据可存入数据库进行分析
【Java爬虫】信息抓取的实现 完整实例(源码)
使用java代码基于MyEclipse开发环境实现爬虫抓取网页中的表格数据,将抓取到的数据在控制台打印出来,需要后续处理的话可以在打印的地方对数据进行操作。包解压后导入MyEclipse就可以使用,在TestCrawTable中右键...
java爬虫抓取新闻.zip
java爬虫需要的jar包。httpclient,jsoup等等。直接导入项目使用 java爬虫需要的jar包。httpclient,jsoup等等。直接导入项目使用 java爬虫需要的jar包。httpclient,jsoup等等。直接导入项目使用java爬虫需要的jar包...