①設定抓取網站的入口網址 [root@red-hat-9 nutch-0.9]# cd /zkl/ir/nutch-0.9/ [root@red-hat-9 nutch-0.9]# mkdir urls [root@red-hat-9 nutch-0.9]# vi urls/urls_crawl.txt 或者不用建立目錄,直接建立一個檔案urls_crawl.txt,我們採用此法, [root@red-hat-9 nutch-0.9]# vi urls_crawl.txt 寫入要抓取(crawl)網站的入口網址,即從此入口開始抓取當前網域名稱下的任何URL頁面,例如: http://english.gu.cas.cn/ag/ ②指定爬取過濾規則 編輯nutch的URL過濾規則檔案conf/crawl-urlfilter.txt [root@red-hat-9 nutch-0.9]# vi conf/crawl-urlfilter.txt 修改 # accept hosts in MY.DOMAIN.NAME # +^http://(/[a-z0-9]*/.)*MY.DOMAIN.NAME/ 為 這是你想要爬取網站的網域名稱,表示爬取當前網站下的所有URL頁面,爬取起始網站在①中已經設定。 ③過濾字元設定 如果爬取網站的url含有以下過濾字元,如 ? 和 = ,而你又需要這些訪問,可以更改過濾表 # skip URLs containing certain characters as probable queries, etc. -[?*!@=] 改為 -[*!@] ④修改conf/nutch-site.xml 修改為 <configuration> <property> <name>http.agent.name</name> http.agent.name屬性 <value>gucas.ac.cn</value> 該值是被抓取網站的名稱,自己設定, 在nutch檢索中會用到 </property> <property> <name>http.agent.version</name> <value>1.0</value> </property> <property> <name>searcher.dir</name> <value>/zkl/ir/nutch-0.9/gucas</value> <description> Path to root of crawl</description> </property> </configuration> 如果沒有配置此agent,爬取時會出現 Agent name not configured! 的錯誤。 ⑤開始爬取 運行crawl命令抓取網站內容 [root@red-hat-9 nutch-0.9]# bin/nutch crawl urls_crawl.txt -dir gucas -depth 50 -threads 5 -topN 1000 >& logs/logs_crawl.log ·-dir dirnames 設定儲存所抓取網頁的目錄. ·-depth depth 表明抓取網頁的層次深度 ·-delay delay 表明訪問不同主機的延時,單位為“秒” ·-threads threads 表明需要啟動的線程數 ·-topN 1000 表明只抓取每一層的前N個URL 在上述命令的參數中,urls_crawl.txt 就是剛才建立的那個包含儲存了抓取網址的檔案urls_crawl.txt的目錄;dir指定抓取內容所存放的目錄,這裡是gucas;depth表示以要抓取網站頂級網址為起點的爬行深度;threads指定並發的線程數;topN表明只抓取每一層的前N個URL;最後的logs/ logs_crawl.log表示把抓取過程顯示的內容儲存在logs目錄下的檔案logs_crawl.log中,以便剖析器的運行情況。 此命令運行完後,將會在nutch-0.9目錄下產生gucas目錄,並存放有抓取的檔案和產生的索引,此外將會在nutch-0.9目錄下剩下logs目錄,此目錄下產生一個檔案logs_crawl.log,儲存的是抓取日誌。 如果gucas在運行前已存在,則運行時將報錯:gucas already exist。建議先刪除這個目錄,或者指定其他的目錄存放抓取的網頁。 完成上述的各步操作,此時資料的抓取順利完成了。 測試:bin/nutch org/apache/nutch/searcher/NutchBean the 查詢關鍵字“the” 。 上面只是抓取單個網站,沒有體現網路蜘蛛從多個網站爬取資料的優點,下面舉例說明爬取多個網站資料時的情況: 在Nutch主目錄下建立檔案multiurls.txt檔案,寫入希望下載的網址列表 http://www.pcauto.com.cn/ http://www.xcar.com.cn/ http://auto.sina.com.cn 修改過濾規則檔案crawl-urlfilter.txt,允許下載任意網站 # accept hosts in MY.DOMAIN.NAME +^ //預設允許所有網站連結 # skip everything else -. 執行抓取命令 [root@red-hat-9 nutch-0.9]# bin/nutch crawl multiurls.txt -dir mutilweb -depth 50 -threads 5 -topN 1000 >& logs/logs_crawl.log 修改conf/nutch-site.xml 修改為: <configuration> <property> <name>http.agent.name</name> http.agent.name屬性 <value>* </value> 該值是網路指蜘蛛名稱, </property> <property> <name>http.agent.version</name> <value>1.0</value> </property> <property> <name>searcher.dir</name> <value>/zkl/ir/nutch-0.9/gucas</value> <description> Path to root of crawl</description> </property> </configuration> 測試:bin/nutch org/apache/nutch/searcher/NutchBean SUV 查詢關鍵字“SUV” 。 --------------------------------------------------------------------- ⑥部署web前端 將nutch主目錄下的nutch-0.9.war 包拷貝到tomcat的webapps目錄下 [root@red-hat-9 nutch-0.9]# cp nutch-0.9.war /zkl/program/apache-tomcat-6.0.18/webapps/ 然後瀏覽器網址 http://localhost:8080/nutch-0.9/ ,此時war包會自動解壓,在tomcat的網頁主目錄webapps下會出現一個nutch-0.9檔案夾。 ⑦修改tomcat中nutch的web配置 vi /zkl/program/apache-tomcat-6.0.18/webapps//nutch-0.9/WEB-INF/classes/nutch-site.xml 將searcher.dir屬性值更改為索引產生的目錄。 <configuration> <property> <name>searcher.dir</name> <value>/zkl/ir/nutch-0.9/gucas</value> <description> Path to root of crawl. This directory is searched (in order) for either the file search-servers.txt, containing a list of distributed search servers, or the directory "index" containing merged indexes, or the directory "segments" containing segment indexes. </description> </property> </configuration> |