最近看了下Nutch,目前Nutch最新版本2.3.1,支持Hbase、MongoDB等存储,但在搭建和测试过程中发现对Mysql 的支持好像有点问题。
后来将Nutch版本改为2.2.1。基于Nutch2.2.1+Mysql 的环境配置过程如下:
1.下载Nutch2.2.1 源码:SVN:https://svn.apache.org/repos/asf/nutch/branches/branch-2.2.1
2.修改Nutch2.2.1 源码中的ivy/ivysetings.xml
- 添加一个源:
<property name="org.restlet"
value="http://maven.restlet.org" override="false"/>- 增加以下红色部分代码
<chain name="default" dual="true">
<resolver ref="local"/> <resolver ref="maven2"/> <resolver ref="apache-snapshot"/> <resolver ref="sonatype"/> <resolver ref="restlet"/> </chain>经过测试,没有增加这个有些包下载不了,可能和网络有关系。
3.修改ivy/ivy.xml
启用以下两个依赖
执行:
ant eclipse -verbose
由于网络带宽问题,整个过程执行了半个小时
执行完成之后如下图所示
发现build文件夹比原来多了很多内容。
5. 打开Eclipse
使用Import 导入Nutch工程
6.配置conf/nutch-site.xml
http.agent.name YourNutchSpider http.accept.language ja-jp, en-us,en-gb,en,zh-cn,zh-tw;q=0.7,*;q=0.3 Value of the “Accept-Language” request header field. This allows selecting non-English language as default one to retrieve. It is a useful setting for search engines build for certain national group. parser.character.encoding.default utf-8 The character encoding to fall back to when no other information is available plugin.folders src/plugin Directories where nutch plugins are located. Each element may be a relative or absolute path. If absolute, it is used as is. If relative, it is searched for on the classpath. storage.data.store.class org.apache.gora.sql.store.SqlStore The Gora DataStore class for storing and retrieving data. Currently the following stores are available: …. generate.batch.id *
gora.datastore.default=org.apache.gora.sql.store.SqlStoregora.datastore.autocreateschema=truegora.sqlstore.jdbc.driver=com.mysql.jdbc.Drivergora.sqlstore.jdbc.url=jdbc:mysql://localhost:3306/nutch?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNullgora.sqlstore.jdbc.user=rootgora.sqlstore.jdbc.password=
CREATE TABLE webpage (
id varchar(256) NOT NULL,headers blob,text longtext DEFAULT NULL,status int(11) DEFAULT NULL,markers blob,parseStatus blob,modifiedTime bigint(20) DEFAULT NULL,prevModifiedTime bigint(20) DEFAULT NULL,score float DEFAULT NULL,typ varchar(32) CHARACTER SET latin1 DEFAULT NULL,batchId varchar(32) CHARACTER SET latin1 DEFAULT NULL,baseUrl varchar(256) DEFAULT NULL,content longblob,title text DEFAULT NULL,reprUrl varchar(256) DEFAULT NULL,fetchInterval int(11) DEFAULT NULL,prevFetchTime bigint(20) DEFAULT NULL,inlinks mediumblob,prevSignature blob,outlinks mediumblob,fetchTime bigint(20) DEFAULT NULL,retriesSinceFetch int(11) DEFAULT NULL,protocolStatus blob,signature blob,metadata blob,PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
9. 配置Crawler.java 的执行参数
10. 修改Hadoop的FileUtil.java
由于windows平台问题,需要修改FileUtil.java 代码,将红色部分注释掉。否则在执行Crawl 过程中会报Hadoop的路径权限错误
1 private static void checkReturnValue(boolean rv, File p, FsPermission permission)2 throws IOException3 {4 //if (!rv)5 // throw new IOException(new StringBuilder().append("Failed to set permissions of path: ").append(p).append(" to ").append(String.format("%04o", new Object[] { Short.valueOf(permission.toShort()) })).toString());6 }
添加需要爬取的网站URL路径,如: http://www.cnblogs.com/
注意:这个urls文件夹与Crawler执行参数的urls 对应。
12.执行Crawler.java 观察Mysql 数据
13.在大多数情况下,网站可能配置了反爬虫的功能robots.txt
Nutch也遵守了该协议,但可以通过修改Nutch的源码绕过反爬虫。
只需要将类FetcherReducer 的以下这个代码注释掉即可
/*
if (!rules.isAllowed(fit.u.toString())) { // unblock fetchQueues.finishFetchItem(fit, true); if (LOG.isDebugEnabled()) { LOG.debug("Denied by robots.txt: " + fit.url); } output(fit, null, ProtocolStatusUtils.STATUS_ROBOTS_DENIED, CrawlStatus.STATUS_GONE); continue; } */