Gecco学习笔记(九)

本文探讨了如何在Gecco中使用htmlunit处理JD商品详情中的Ajax请求,介绍了htmlunit的原理和下载方式,并分析了其效率和rhino引擎的兼容性。重点讨论了htmlunit在自动化抓取中的便捷与局限性。
摘要由CSDN通过智能技术生成

2021SC@SDUSC

简单说明一下Gecco中的htmlunit。

htmlunit是一款开源的java 页面分析工具,读取页面后,可以有效的使用htmlunit分析页面上的内容。项目可以模拟浏览器运行,被誉为java浏览器的开源实现。这个没有界面的浏览器,运行速度也是非常迅速的。htmlunit采用的是rhino作为javascript的解析引擎

下载

 <dependency>
      <groupId>com.geccocrawler</groupId>
      <artifactId>gecco-htmlunit</artifactId>
      <version>x.x.x</version>
  </dependency>

JD的商品详情信息里的价格的信息是通过ajax异步请求而来的,之前是利用@Ajax注解的方式实现的。这里用htmlunit来自动完成ajax请求。

@Gecco(matchUrl="http://item.jd.com/{code}.html", pipelines="consolePipeline", downloader="htmlUnitDownloder")
public class JDDetail implements HtmlBean {

	private static final long serialVersionUID = -377053120283382723L;

	@RequestParameter
	private String code;
	
	@Text
	@HtmlField(cssPath=".p-price")
	private String price;
	
	@Text
	@HtmlField(cssPath="#name > h1")
	private String title;
	
	@Text
	@HtmlField(cssPath="#p-ad")
	private String jdAd;
	
	@HtmlField(cssPath="#product-detail-2")
	private String detail;

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public String getJdAd() {
		return jdAd;
	}

	public void setJdAd(String jdAd) {
		this.jdAd = jdAd;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDetail() {
		return detail;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public static void main(String[] args) throws Exception {
		HttpRequest request = new HttpGetRequest("http://item.jd.com/1455427.html");
		request.setCharset("GBK");
		GeccoEngine.create()
		.classpath("com.geccocrawler.gecco.htmlunit")
		//开始抓取的页面地址
		.start(request)
		//开启几个爬虫线程
		.thread(1)
		.timeout(1000)
		.run();
	}
}

优缺点 使用htmlunit确实能省去很多工作,但是htmlunit也存在很多弊端:

1、效率低下,使用htmlunit后,下载器要将所有js一并下载下来,同时要执行所有js代码,下载一个页面有时需要5~10秒。

2、rhino引擎对js的兼容问题,rhino的兼容性还是存在不少问题的。如果大家在抓取时不想看到这些error日志输出可以配置log4j:

log4j.logger.com.gargoylesoftware.htmlunit=OFF

Gecco是什么 Gecco是一款用java语言开发的轻量化的易用的网络爬虫Gecco整合了jsoup、httpclient、fastjson、spring、htmlunit、redission等优秀框架,让您只需要配置一些jquery风格的选择器就能很快的写出一个爬虫Gecco框架有优秀的可扩展性,框架基于开闭原则进行设计,对修改关闭、对扩展开放。同时Gecco基于十分开放的MIT开源协议,无论你是使用者还是希望共同完善Gecco的开发者,欢迎pull request。如果你喜欢这款爬虫框架请star 或者 fork!参考手册架构图: 主要特征  简单易用,使用jquery风格的选择器抽取元素  支持页面中的异步ajax请求  支持页面中的javascript变量抽取  利用Redis实现分布式抓取,参考gecco-redis  支持结合Spring开发业务逻辑,参考gecco-spring  支持htmlunit扩展,参考gecco-htmlunit  支持插件扩展机制  支持下载时UserAgent随机选取  支持下载代理服务器随机选取 使用手册:http://www.geccocrawler.com/tag/sysc/快速入门:@Gecco(matchUrl="https://github.com/{user}/{project}", pipelines="consolePipeline") public class MyGithub implements HtmlBean {     private static final long serialVersionUID = -7127412585200687225L;     @RequestParameter("user")     private String user;     @RequestParameter("project")     private String project;     @Text     @HtmlField(cssPath=".repository-meta-content")     private String title;     @Text     @HtmlField(cssPath=".pagehead-actions li:nth-child(2) .social-count")     private int star;     @Text     @HtmlField(cssPath=".pagehead-actions li:nth-child(3) .social-count")     private int fork;     @Html     @HtmlField(cssPath=".entry-content")     private String readme;     public String getReadme() {         return readme;     }     public void setReadme(String readme) {         this.readme = readme;     }     public String getUser() {         return user;     }     public void setUser(String user) {         this.user = user;     }     public String getProject() {         return project;     }     public void setProject(String project) {         this.project = project;     }     public String getTitle() {         return title;     }     public void setTitle(String title) {         this.title = title;     }     public int getStar() {         return star;     }     public void setStar(int star) {         this.star = star;     }     public int getFork() {         return fork;     }     public void setFork(int fork) {         this.fork = fork;     }     public static void main(String[] args) {         GeccoEngine.create()         .classpath("com.geccocrawler.gecco.demo")         .start("https://github.com/xtuhcy/gecco")         .thread(1)         .interval(2000)         .loop(true)         .mobile(false)         .start();     } }demo地址:教您使用java爬虫gecco抓取JD全部商品信息(一)教您使用java爬虫gecco抓取JD全部商品信息(二)教您使用java爬虫gecco抓取JD全部商品信息(三)集成Htmlunit下载页面爬虫的监控一个完整的例子,分页处理,结合spring,mysql入库 标签:网络爬虫  开源爬虫
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值