1,下载Nutch0.9
2,到下面两个页面去下载两个jar文件,他们分别是:
http://nutch.cvs.sourceforge.net/nutch/nutch/src/plugin/parse-mp3/lib/
http://nutch.cvs.sourceforge.net/nutch/nutch/src/plugin/parse-rtf/lib/
3,修改 Nutch\conf 目录下的 nutch-site.xml 和crawl-urlfilter.txt 两个文件,具体的修改方法如下:
(1) nutch-site.xml:
在<configuration></configuration>中间插入以下内容:
<property> <name>http.agent.name</name> <value>Jennifer</value> <description>HTTP 'User-Agent' request header. MUST NOT be empty - please set this to a single word uniquely related to your organization. NOTE: You should also check other related properties: http.robots.agents http.agent.description http.agent.url http.agent.email http.agent.version and set their values appropriately. </description> </property> <property> <name>http.agent.description</name> <value>Jennifer</value> <description>Further description of our bot- this text is used in the User-Agent header. It appears in parenthesis after the agent name. </description> </property> <property> <name>http.agent.url</name> <value>Jennifer</value> <description>A URL to advertise in the User-Agent header. This will appear in parenthesis after the agent name. Custom dictates that this should be a URL of a page explaining the purpose and behavior of this crawler. </description> </property> <property> <name>http.agent.email</name> <value>Jennifer</value> <description>An email address to advertise in the HTTP 'From' request header and User-Agent header. A good practice is to mangle this address (e.g. 'info at example dot com') to avoid spamming. </description> </property>
并修改<value></value> 中间的值,这里的设置是因为Nutch 遵守了robots 协议,在获取response 时,把自己的相关信息提交给被爬行的网站,以供识别。所以设置成任何你喜欢的值都可以的。
(2) crawl-urlfilter.txt:
找到"+^http://([a-z0-9]*\.)* MY.DOMAIN.NAME/” ,将其中的' MY.DOMAIN.NAME/' 直接删除。
为了后面的方便,建议在修改完成后将conf 文件夹复制一下,并保存在硬盘的其他的地方。
4,准备工作做好以后就可以在Eclipse中配置Nutch了。打开Eclipse后,我们开始建立Java project。具体的做法是:
File > New > Project > Java project创建Eclipse项目,给我们的项目命名,然后选择Create project from existing source,并指向你的Nutch目录。
5,点击Next后我们就能看到用于定义Java build的设置环境。在第一个选项卡source下面我们可以看到三个连接,我们需要选择第三个:Add project ‘Nutch’ to build path。括号中的我没遇到(点击之后我们就选择conf,完成以上步骤之后我们就将conf加入到了classpath中。注意:这里不是指上面的第三 个选项卡。)
6,现在需要我们选择Default output folder,在这里我们必须选择Nutch/conf作为它的Default output folder,否则就会找不到crawl-urlfilter.txt,从而不能完成网站的爬行,就会出现我之前一只没有解决的问题,会出现以下提示:
Generator: 0 records selected for fetching, exiting ...
Stopping at depth=0 - no more URLs to fetch.
No URLs to fetch - check your seed list and URL filters.
7,点击Finish,这时我们就完成了前续的配置工作,这时我们就可点击运行,选择Java Application点击OK,开始让Eclipse自己寻找项目的主类。我们选择Crawl-org.apache.nutch.crawl,点击OK。
8,这时我们可以回到Nutch\conf目录下看下,我们可以看到里面的内容发生了改变,这时就需要我们还原原来conf下的内容,这时我们在第 2步中另外保存的conf文件夹就起到了作用。(当然,新生成的东西并不影响我们的后续爬行工作,我们可以只考虑将原来conf下的内容复制回来,但是我 个人觉得为了文件夹的管理和查找方便,我建议将新生成的内容全部删除后在将原来conf下的内容复制回来。还有一点需要我们注意,conf里面不可以有 org文件夹,如果有的话delete,否则的话会影响index。)当然,拷回来以后得记得刷新一下哦!
9,点击菜单中的Run > Open run dialog,选择Java Application分支,选中Arguments选项卡。
在Program arguments中填写爬行命令,如:myurl -dir d:/index -depth 2 -topN 50
在VM arguments中填写:-Dhadoop.log.dir=logs -Dhadoop.log.file=hadoop.log
填写完成后,点击apply完成提交。由于我们到目前为止还没有建立需要我们爬行的url地址,所以我们在提交完成后,点击close,然后我们就可以建立需要Nutch爬行的url地址。
(1)在nutch-0.9解压后的根目录下,新建myurl 文件夹,然后写个名字是“nutch"的文件,该文件中直接加入需要爬的主站,如
http://www.163.com
(2)conf目录下,修改crawl-urlfilter.txt,#号是注释,这里爬虫过滤掉不是+^http://bbs.xldy.cn/的地址,如
# accept hosts in MY.DOMAIN.NAME +^http://bbs.xldy.cn/ # +^http://([a-z0-9]*\.)*xldy.cn/