在Windows环境下安装和配置Nutch 0.9
一、运行环境:
1.?????? 操作系统:Windows XP
2.?????? Java 1.6,设置JAVA_HOME等环境变量
鼠标右键单击我的电脑→属性→高级→环境变量,如下图示:

1)?????? 单击系统变量列表框下面的新建按钮,新建java_home环境变量。
2)?????? 单击系统变量列表框下面的新建按钮,新建classpath环境变量。
3)?????? 在系统变量中找到path变量,并在原有path值的基础上添加环境变量。
例如:
1)?????? java_home=E:\Java\Jdk
2)?????? classpath=.\;E:\Java\JDK\lib\dt.jar;E:\Java\JDK\lib\tools.jar;E:\Java\JDK\jre\lib(注意:“.\;”不能少)
3)?????? path=……;E:\Java\Jdk\bin(注意:是添加,不是更改)
3.?????? Cygwin,由于Nutch提供的脚本只能在Linux环境下使用,所以使用Cygwin来虚拟Linux命令。下载地址:http://www.cygwin.cn/
4.?????? Nutch 0.9
5.?????? Tomcat 6.0
?
?
二、Nutch的安装和配置:
1.?????? 安装Cygwin(我这里装到E:\Cygwin)。
2.?????? 将Nutch解压缩后放置到Cygwin的根目录(或者:\home\用户名\)的目录下。
解压的方法是:
1)?????? 为了便于操作,建议将Nutch 0.9的安装包拷贝到Cygwin\home\用户名\目录下,这是由于使用Cygwin进入操作界面是的默认目录是:\home\用户名。我们可以使用pwd命令来查看当前的目录。当然也可以拷贝到Cygwin的根目录下,这样就需要我们使用cd命令来退到Cygwin的根目录。启动Cygwin,我们就可以看到以下界面,具体操作如下:
2)?????? 使用dir命令可以查看当前目录下含有的文件,我们就可以看到Nutch 0.9的安装包,这时我们就可以将Nutch 0.9解压出来。输入命令:tar xvf nutch-0.9.tar.gz
3)?????? 在Cygwin环境下进入Nutch-0.9目录下,使用cd命令可以跳转到指定的目录。

为方便目录管理,我采用的目录结构为:E:\Cygwin\Nutch\nutch-0.9
使用命令bin/Nutch进行测试,正常的情况下出现的结果是:

3.?????? 进行抓取网站的测试,以抓取http://www.163.com为例
1)?????? 新建一个文件夹myUrl,在文件夹中新建一个文本文件,在该文本文件中输入http://www.163.com保存。另外再建立一个文件夹myLog,该文件夹是用来存放使用Nutch抓取的网页信息。这个两个文件夹都可以放在任何地方。我的目录结构如下图示:

2)?????? 下面还需要修改几个文件,建议使用文本编辑器,例如我用的就是UltrEdit。打开nutch-0.9\conf\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时,把自己的相关信息提交给被爬行的网站,以供识别。所以设置成任何你喜欢的值都可以的。
?
3)?????? 打开Nutch-0.9\conf\crawl-urlfilter.txt文件
找到"+^http://([a-z0-9]*\.)* MY.DOMAIN.NAME/”,将其中的'MY.DOMAIN.NAME/'直接删除。
4)?????? 运行爬虫,在Cygwin输入以下命令,如下图示:

其中:crawl后面跟的是用于存储你想要爬行的网站的网址的目录和文件。-dir后面跟的是用于存储nutch抓取的网站的内容的目录。注意:该目录当前必须不存在,它用于指出存放爬行后结果的目录。-depth表示爬行的深度。最后一个参数是指明日志文件存放的目录和文件。
运行结束后,你可以打开日志文件查看爬虫运行的详细过程。
4.?????? 在tomcat上运行Nutch
1)?????? 把nutch-0.9.war拷贝到Tomcat\webapps\下面
2)?????? 在浏览器中输入http://localhost:8080/nutch-0.9/
3)?????? 修改Tomcat\webapps\nutch-0.9\WEB-INF\classes\nutch-site.xml文件,并在<configuration></configuration>中添加以下内容:
?
<property>
<name>searcher.dir</name>
<value>E:\Cygwin\nutch\mydir</value>
</property>
?
?
?
?
?
其中<value></value>中间的值一定是前面爬行网站是用于存放结果的目录。
4)?????? 为了支持中文的搜索,修改Tomcat\conf\server.xml。找到对应的地方修改成以下内容:
?
<Connector port="8080" protocol="HTTP/1.1"
?????????????? connectionTimeout="20000"
?????????????? redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true"/>
?
?
?
?
?
?
5.?????? 在浏览器中输入http://localhost:8080/nutch-0.9
?
?
?
文章链接转自:http://apps.hi.baidu.com/share/mine
?
?