---
title: "毕业设计3－－－Nutch的使用"
author: "fullfocus"
date: "2007-03-02T00:53:08Z"
tags: []
categories: ["企业架构"]
slug: "3-nutch"
draft: false
lastmod: "2007-03-02T00:53:08Z"
id: 56860
url: "https://fullfocus.iteye.com/blog/56860"
---

今天研究了Nutch， 差不多已经好几个小时了， 到现在还没有搞定，也这么晚了， 先记录下来，明天继续吧。

       一开始很多时间都浪费在了cygwin的安装上了，bs这个软件的开发者了， 一个不伦不类的软件安装程序，安装的时候还要从网上下载东东。。。。。不过最后终于装成功了， 先下载到本地后，再安装的（建议 下载站点中选 TW的比较块）。

   下面是我安装CYGWIN和NUTCH的过程， 都块成功了， 但最后卡在了用户查询界面， 输入东西什么都查不出来，不知怎么回事。

                                            **[NUTCH的大致原理如下](http://www.blogjava.net/calvin/archive/2006/08/09/62507.html)：**![](http://wiki.media-style.com/download/attachments/552/workflow.png)

安装步骤参考了[该文章](http://www.blogjava.net/redmu/archive/2006/10/18/76016.html) 

**一、环境：**  
      1.操作系统：windowsXp,windows2000+  
      2.javaVM：java1.5.x，设置JAVA\_HOME到环境变量  
      3.[cygwin](http://www.cygwin.com/),当然这个不是必需的，只是nutch提供的脚本只能在shell环境下使用，所以使用cygwin来虚拟shell命令。  
      4.nutch版本：0.8  
      5.tomcat：5.0

**二、cygwin的安装：**

      cygwin的安装在[Nutch在Windows中安装之细解](http://www.window07.com/dev/code/Java/2006-3-5/k79172.htm)一文中有较为详细的介绍，此处不再介绍安装步骤，只介绍安装后需要如何判断是否能够使用：在cygwin的安装目录下，查找x:\\cygwin\\cygwin\\bin\\sh.exe，存在此命令即可使用。  
      cygwin在删除后会发现无法再次成功安装的问题，可以通过注册表内的查找功能，删除所有包含cygwin内容的键值即可。

**三、nutch的安装和配置：**

      1。从[http://lucene.apache.org/nutch/release/](http://lucene.apache.org/nutch/release/)下载0.8或更高的版本，解压缩后，放置到cygwin的根目录下，如图：

 ![](http://photo.bababian.com/20061018/5F0477F16A6B5A556EDADCC3AE05D656_240.jpg)

     图中可以看到nutch目录在cygwin的根目录下。  
       
    2。在nutch/bin下，建立urls目录，然后建立一个url.txt文件，在url.txt文件内写入一个希望爬行的url，例如：[www.sina.com.cn](http://www.sina.com.cn/)，目录结构如图：

 ![](http://photo.bababian.com/20061018/C45ADF943539E6E64F63C4D32D3FE925_240.jpg)

       
      3。打开nutch\\conf\\crawl-urlfilter.txt文件，把MY.DOMAIN.NAME字符替换为url.txt内的url的域名，其实更简单点，直接删除MY.DOMAIN.NAME这几个字就可以了，也就是说，只保存+^http://(\[a-z0-9]\*\\.)\*这几个字就可以了，表示所有http的网站都同意爬行。

      4 。打开nutch\\conf\\conf/nutch-site.xml文件，在&lt;configuration&gt;&lt;/configuration&gt;内插入一下内容：

&lt;property&gt;&lt;o:p&gt;&lt;/o:p&gt;

  &lt;name&gt;http.agent.name&lt;/name&gt;&lt;o:p&gt;&lt;/o:p&gt;

  &lt;value&gt;&lt;/value&gt;&lt;o:p&gt;&lt;/o:p&gt;

  &lt;description&gt;HTTP 'User-Agent' request header. MUST NOT be empty - &lt;o:p&gt;&lt;/o:p&gt;

  please set this to a single word uniquely related to your organization.&lt;o:p&gt;&lt;/o:p&gt;

&lt;o:p&gt; &lt;/o:p&gt;

  NOTE: You should also check other related properties:&lt;o:p&gt;&lt;/o:p&gt;

&lt;o:p&gt; &lt;/o:p&gt;

     http.robots.agents&lt;o:p&gt;&lt;/o:p&gt;

     http.agent.description&lt;o:p&gt;&lt;/o:p&gt;

     http.agent.url&lt;o:p&gt;&lt;/o:p&gt;

     http.agent.email&lt;o:p&gt;&lt;/o:p&gt;

     http.agent.version&lt;o:p&gt;&lt;/o:p&gt;

&lt;o:p&gt; &lt;/o:p&gt;

  and set their values appropriately.&lt;o:p&gt;&lt;/o:p&gt;

&lt;o:p&gt; &lt;/o:p&gt;

  &lt;/description&gt;&lt;o:p&gt;&lt;/o:p&gt;

&lt;/property&gt;&lt;o:p&gt;&lt;/o:p&gt;

&lt;o:p&gt; &lt;/o:p&gt;

&lt;property&gt;&lt;o:p&gt;&lt;/o:p&gt;

  &lt;name&gt;http.agent.description&lt;/name&gt;&lt;o:p&gt;&lt;/o:p&gt;

  &lt;value&gt;&lt;/value&gt;&lt;o:p&gt;&lt;/o:p&gt;

  &lt;description&gt;Further description of our bot- this text is used in&lt;o:p&gt;&lt;/o:p&gt;

  the User-Agent header.  It appears in parenthesis after the agent name.&lt;o:p&gt;&lt;/o:p&gt;

  &lt;/description&gt;&lt;o:p&gt;&lt;/o:p&gt;

&lt;/property&gt;&lt;o:p&gt;&lt;/o:p&gt;

&lt;o:p&gt; &lt;/o:p&gt;

&lt;property&gt;&lt;o:p&gt;&lt;/o:p&gt;

  &lt;name&gt;http.agent.url&lt;/name&gt;&lt;o:p&gt;&lt;/o:p&gt;

  &lt;value&gt;&lt;/value&gt;&lt;o:p&gt;&lt;/o:p&gt;

  &lt;description&gt;A URL to advertise in the User-Agent header.  This will &lt;o:p&gt;&lt;/o:p&gt;

   appear in parenthesis after the agent name. Custom dictates that this&lt;o:p&gt;&lt;/o:p&gt;

   should be a URL of a page explaining the purpose and behavior of this&lt;o:p&gt;&lt;/o:p&gt;

   crawler.&lt;o:p&gt;&lt;/o:p&gt;

  &lt;/description&gt;&lt;o:p&gt;&lt;/o:p&gt;

&lt;/property&gt;&lt;o:p&gt;&lt;/o:p&gt;

&lt;o:p&gt; &lt;/o:p&gt;

&lt;property&gt;&lt;o:p&gt;&lt;/o:p&gt;

  &lt;name&gt;http.agent.email&lt;/name&gt;&lt;o:p&gt;&lt;/o:p&gt;

  &lt;value&gt;&lt;/value&gt;&lt;o:p&gt;&lt;/o:p&gt;

  &lt;description&gt;An email address to advertise in the HTTP 'From' request&lt;o:p&gt;&lt;/o:p&gt;

   header and User-Agent header. A good practice is to mangle this&lt;o:p&gt;&lt;/o:p&gt;

   address (e.g. 'info at example dot com') to avoid spamming.&lt;o:p&gt;&lt;/o:p&gt;

  &lt;/description&gt;&lt;o:p&gt;&lt;/o:p&gt;

&lt;/property&gt;&lt;o:p&gt;&lt;/o:p&gt;

     把&lt;name&gt;XXX&lt;/name&gt;之间的内容替换为其他字符，当然就算是不替换也无所谓，这里的设置，是因为nutch遵守了robots协议，在获取response时，把自己的相关信息提交给被爬行的网站，以供识别。

   以上配置，是爬取intranet的配置方式。

    **四、执行nutch**

   由于配置nutch采用的是单独网站的配置方式，所以执行上我们也采用的是单网查询，全网查询在以后的内容中介绍。

   先看一看nutch给出的命令：nutch crawl urls -dir crawl -depth 3 -topN 50  
   crawl：通知nutch.jar，执行crawl的main方法。  
   urls：存放需要爬行的url.txt文件的目录，注意，这个名字需要和你的文件夹目录相同，如果你的文件夹为search，那这里也应该改成search。  
   -dir crawl：爬行后文件保存的位置，可以在nutch/bin目录下找到。  
   -depth 3：爬行次数，或者成为深度，不过还是觉得次数更贴切，建议测试时改为1。  
   -topN 50：一个网站保存的最大页面数。

      执行命令的步骤：  
      1。进入cygwin界面。  
      2。使用cd命令，进入nutch\\bin路径下。  
      3。执行：sh nutch crawl urls -dir crawl -depth 3 -topN 50

   具体的爬行日志可以在nutch/logs目录下看到，注意查找“INFO  fetcher.Fetcher - fetching [http://XXXXXXX](http://xxxxxxx/)”这样的内容，这里是抓去过程日志。

   **五、查询搜索：（目前我还没有通过此步,输入关键字老是没有结果？？？？）**  
   nutch 提供了类似google、baidu的网页页面，在nutch压缩包下找到nutch-0.8.war文件，放到tomcat/webapps目录下，修改webapps/nutch/WEB-INF/classes/nutch-site.xml文件内容如下：

&lt;property&gt;  
&lt;name&gt;searcher.dir&lt;/name&gt;  
&lt;value&gt;C:\\\\cygwin\\\\nutch\\\\bin\\\\crawl&lt;/value&gt;  
&lt;/property&gt;

   &lt;value/&gt;的内容是刚才爬行后的crawl目录位置，提供给客户端来查询。

　　配置完成后，启动ｔｏｍｃａｔ，输入[http://localhost:8080/nutch](http://localhost:8080/nutch)，输入关键字，就会看到结果了，下图是我抓去ｗａｐ网站的测试结果：

![](http://photo.bababian.com/20061018/A0B4515C1A9808DA38905E6E4C7599CC_240.jpg)

   六、总结：  
   ntuch 提供了一个高效、开源、易操作的搜索引擎，内部有许多细微之处都是值得借鉴的，例如采用了hadoop的分布式文件系统，类似eclipse的插件技术， apache的httpclient来访问网站，org.cyberneko.html得HtmlParse来解析页面等等，在以后会逐个介绍。

-----------------------------------------------------------------------------------------------------------------------------------------------明天继续，呵呵