现在的位置: 首页 > 综合 > 正文

开源JAVA爬虫crawler4j源码分析 – 1 开个头

2014年04月05日 ⁄ 综合 ⁄ 共 3264字 ⁄ 字号 评论关闭

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

http://www.open-open.com/68.htm

发现用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("<meta .*charset=[\"]*[^\"]+", "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="+contentCharset);
			contentData = data.getBytes(contentCharset);
		}
	}
	

2014.02.18补充:

经测试,以下解决乱码方案可以使乱码率降到更低:

		contentData = EntityUtils.toByteArray(entity);
		//中文乱码
		if(charset != null) {
			String data = new String(contentData,contentCharset);

			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(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("<head>", "<head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset="+contentCharset+"\">");
			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("<head>", "<head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset="+contentCharset+"\">");
				contentData = data.getBytes(contentCharset);
			} else if (isEncodeUTF && !isEncodeGBK) {
				contentCharset = "UTF-8"; 
				data = new String(contentData,contentCharset);
				data = data.replaceFirst("<head>", "<head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset="+contentCharset+"\">");
				contentData = data.getBytes(contentCharset);
			}
		}

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

抱歉!评论已关闭.