Windows環境下nutch0.9的配置

來源:互聯網
上載者:User
一、簡介

  Nutch是一個開源的Web搜尋引擎。

       主要分為兩個部分:爬蟲crawler和查詢searcher,兩者之間的介面是索引。

 

二、需要的軟體
  • JDK1.6

  • Tomcat6.0

  • cygwin

    使用原因:  運行Nutch內建的指令碼命令需要Linux的環境,使用cygwin來類比該環境。cygwin是在windows平台上啟動並執行unix類比環境。

  • nutch0.9

  三、安裝與配置    1. JDK
  • 下載jdk

  • 安裝目錄:d:/Java/jdk1.6.0_21

  • 修改環境變數:

JAVA_HOME= d:/Java/jdk1.6.0_21

CLASSPATH= d:/Java/jdk1.6.0_21/lib/dt.jar; d:/Java/jdk1.6.0_21/lib/tool.jar

PATH= %JAVA_HOME%/bin

  • 測試命令:Java -version

    2. Tomcat6.0
  • 下載tomcat
  • 安裝目錄:d:/tomcat6
  • 安裝測試:

            a)  啟動tomcat:在DOS下,cd d:/tomcat6/bin/startup.bat

            b)  瀏覽器地址欄輸入: http://localhost:8080/    出現tomcat首頁則成功

  • 更改d:/tomcat6/conf /tomcat-users.xml內容

 

<tomcat-users>

  <role rolename="manager"/>

  <user username="tomcat" password="tomcat"  roles="manager"/>

</tomcat-users>

 

    3. Cygwin
  • :http://www.cygwin.cn/

  • 安裝目錄:d:/cygwin

               注意:在選擇下載網站需要輸入如下網址,並在最後選擇本地磁碟安裝:http://www.cygwin.cn/pub/

  • 測試cygwin

   4. Nutch
  • 下載nutch版本:nutch-0.9

  • 設定Nutch的環境變數:

增加環境變數NUTCH_JAVA_HOME :  NUTCH_JAVA_HOME= d:/Java/jdk1.6.0_21

 

修改環境變數PATH :  PATH= %JAVA_HOME%/bin; % NUTCH_JAVA_HOME %/bin

  • 將Nutch解壓縮後放置到Cygwin的/home/使用者名稱目錄下。解壓的方法是: 
  1. 為了便於操作,建議將Nutch 0.9的安裝包拷貝到Cygwin/home/使用者名稱/目錄下,這是由於使用Cygwin進入操作介面是的預設目錄是:/home/使用者名稱。我們可以使用pwd命令來查看當前的目錄。
  2.  使用dir命令可以查看目前的目錄下含有的檔案,我們就可以看到Nutch 0.9的安裝包,這時我們就可以將Nutch 0.9解壓出來。輸入命令:tar xvf nutch-0.9.tar.gz

 

  • 使用命令bin/nutch進行測試,成功介面如下:

                

 

  四、抓取網頁資料

      

       進行抓取網站的測試,以抓取http://www.163.com為例

      (1) 指定爬蟲規則 

 

  • 在使用者名稱目錄下建立一個檔案夾myUrl,在檔案夾中建立一個文字檔,在該文字檔中輸入http://www.163.com儲存。另外再在該目錄下建立一個檔案夾myLog,該檔案夾是用來存放使用Nutch抓取的網頁資訊。
  • 下面還需要修改幾個檔案,建議使用文字編輯器,例如我用的就是UltrEdit。
  •  

                 a)開啟nutch-0.9/conf/nutch-site.xml檔案。 在<configuration></configuration>中間插入以下內容: <property><br /><name>http.agent.name</name><br /><value>XXXX</value><br /><description>HTTP 'User-Agent' request header. MUST NOT be empty please set this to a single word uniquely related to your organization.</p><p>NOTE: You should also check other related properties:</p><p> http.robots.agents<br /> http.agent.description<br /> http.agent.url<br /> http.agent.email<br /> http.agent.version</p><p>and set their values appropriately.</p><p></description><br /></property></p><p><property><br /><name>http.agent.description</name><br /><value>XXXX</value><br /><description>Further description of our bot- this text is used in<br />the User-Agent header. It appears in parenthesis after the agent name.<br /></description><br /></property></p><p><property><br /><name>http.agent.url</name><br /><value>XXXX</value><br /><description>A URL to advertise in the User-Agent header. This will<br /> appear in parenthesis after the agent name. Custom dictates that this<br /> should be a URL of a page explaining the purpose and behavior of this<br /> crawler.<br /></description><br /></property></p><p><property><br /><name>http.agent.email</name><br /><value>XXXX</value><br /><description>An email address to advertise in the HTTP 'From' request<br /> header and User-Agent header. A good practice is to mangle this<br /> address (e.g. 'info at example dot com') to avoid spamming.<br /></description><br /></property> 

    並修改<value></value>中間的值,這裡的設定是因為Nutch遵守了robots協議,在擷取response時,把自己的相關資訊提交給被爬行的網站,以供識別。所以設定成任何你喜歡的值都可以的。

     

    b)開啟Nutch-0.9/conf/crawl-urlfilter.txt檔案,找到+^http://([a-z0-9]*/.)* MY.DOMAIN.NAME/將其中的'MY.DOMAIN.NAME/'直接刪除

    (2)開始爬取

              在Cygwin輸入以下命令:

                  

                                 bin/nutch crawl myUrl -dir myDir -depth 2 >& myLog/crawl1.log 

     

     

    其中:crawl後面跟的是用於儲存你想要爬行的網站的網址的目錄和檔案。-dir後面跟的是用於儲存nutch抓取的網站的內容的目錄。注意:該目錄當前必須不存在,它用於指出存放爬行後結果的目錄。-depth表示爬行的深度。最後一個參數是指明記錄檔存放的目錄和檔案。運行結束後,你可以開啟記錄檔查看爬蟲啟動並執行詳細過程。

     

    五、部署Web前端
    •  把nutch-0.9.war拷貝到tomcat/webapps/下面;
    • 在瀏覽器中輸入http://localhost:8080/nutch-0.9/,則tomcat會將nutch-0.9.war自動解壓到webapps目錄下;
    • 修改Tomcat/webapps/nutch-0.9/WEB-INF/classes/nutch-site.xml檔案,並在<configuration></configuration>中添加以下內容:<property><br /><name>searcher.dir</name><br /><value>d:/cygwin/home/使用者名稱/myDir</value><br /></property> 

    其中<value></value>中間的值一定是前面爬行網站是用於存放結果的目錄。

     

    • 為了支援中文的搜尋,修改Tomcat/conf/server.xml。找到對應的地方修改成以下內容:

    <Connector port="8080" protocol="HTTP/1.1"

                   connectionTimeout="20000"

                   redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true"/>

     

    •  在瀏覽器中輸入http://localhost:8080/nutch-0.9
    相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.