一、簡介
Nutch是一個開源的Web搜尋引擎。
主要分為兩個部分:爬蟲crawler和查詢searcher,兩者之間的介面是索引。
二、需要的軟體
三、安裝與配置 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
2. Tomcat6.0
<tomcat-users>
<role rolename="manager"/>
<user username="tomcat" password="tomcat" roles="manager"/>
</tomcat-users>
3. Cygwin
4. 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/使用者名稱目錄下。解壓的方法是:
- 為了便於操作,建議將Nutch 0.9的安裝包拷貝到Cygwin/home/使用者名稱/目錄下,這是由於使用Cygwin進入操作介面是的預設目錄是:/home/使用者名稱。我們可以使用pwd命令來查看當前的目錄。
- 使用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