java爬虫关键字索引代码_开源JAVA爬虫crawler4j源码分析 – 1 开个头 | 学步园

最近有需要用到爬虫程序,翻看了一下互联网上关于爬虫的一些介绍及一些开源的网络爬虫:

发现用nutch的人比较多,随即拿来使用。之后觉得nutch太过复杂,适合大规模海量数据的爬取,我目前还没有这种需求,留着以后再做研究!

逐个看了看其它几个小的开源爬虫,发现太老不更新就是文档太少。

crawler4j是一个短小精悍的爬虫,且非常容易使用,项目主页:https://code.google.com/p/crawler4j/。当然,code.google.com经常会被间歇性的墙掉,要有点耐心!

用Git把源码下下来导入eclipse,JDK必需是1.7,否则报new ArrayList<>()不支持错误。

爬虫业务逻辑在src/main/java下,可直接运行src/test/java下edu.uci.ics.crawler4j.examples.basic.BasicCrawlController

很简洁,总共就35个类,架构也很清晰:

edu.uci.ics.crawler4j.crawler 基本逻辑和配置

edu.uci.ics.crawler4j.fetcher 爬取

edu.uci.ics.crawler4j.frontier URL队列相关

edu.uci.ics.crawler4j.parser 对爬取结果进行解析

edu.uci.ics.crawler4j.robotstxt 检查robots.txt是否存在

edu.uci.ics.crawler4j.url URL相关,主要是WebURL

edu.uci.ics.crawler4j.util 是工具类

提前说一下crawler4j中文乱码问题,爬取暂时没有发现有乱码问题,解析时出现乱码。原因是tika在解析HTML时会已meta charset编码做为默认编码,当该编码与实际编码不一致时则会出现乱码,这里将meta charset改为正确的编码,在Page.load中修改如下:

/**

* Loads the content of this page from a fetched

* HttpEntity.

*/

public void load(HttpEntity entity) throws Exception {

contentType = null;

Header type = entity.getContentType();

if (type != null) {

contentType = type.getValue();

}

contentEncoding = null;

Header encoding = entity.getContentEncoding();

if (encoding != null) {

contentEncoding = encoding.getValue();

}

Charset charset = ContentType.getOrDefault(entity).getCharset();

if (charset != null) {

contentCharset = charset.displayName();

}

contentData = EntityUtils.toByteArray(entity);

//中文乱码

//if(contentCharset != null) contentData = new String(contentData, contentCharset).getBytes();

if(charset != null) {

String data = new String(contentData,contentCharset);

data = data.replaceFirst("

if(!java.nio.charset.Charset.forName("UTF-8").newEncoder().canEncode(data)) isEncodeUTF = false;

if(contentCharset.equalsIgnoreCase("GBK") && !isEncodeGBK) {

contentCharset = "UTF-8";

data = new String(contentData,contentCharset);

} else if (contentCharset.equalsIgnoreCase("UTF-8") && !isEncodeUTF) {

contentCharset = "GBK";

data = new String(contentData,contentCharset);

}

data = data.replaceFirst("

", " ");

contentData = data.getBytes(contentCharset);

} else {

String data = new String(contentData);

boolean isEncodeGBK = true, isEncodeUTF = true;

if(!java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(data)) isEncodeGBK = false;

if(!java.nio.charset.Charset.forName("UTF-8").newEncoder().canEncode(data)) isEncodeUTF = false;

if(isEncodeGBK && !isEncodeUTF) {

contentCharset = "GBK";

data = new String(contentData,contentCharset);

data = data.replaceFirst("

", " ");

contentData = data.getBytes(contentCharset);

} else if (isEncodeUTF && !isEncodeGBK) {

contentCharset = "UTF-8";

data = new String(contentData,contentCharset);

data = data.replaceFirst("

", " ");

contentData = data.getBytes(contentCharset);

}

}

代码比较冗余且没有注释,有点忙,见谅!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值