天 霽

          那天風(fēng)大,搜搜的
          posts - 4, comments - 36, trackbacks - 1, articles - 1
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          ???nutch作為開(kāi)源代碼,為熱愛(ài)搜索引擎的開(kāi)發(fā)人員們提供了很好的學(xué)習(xí)平臺(tái),0.8版本開(kāi)始,采用了Hadoop作為自己的分布式文件系統(tǒng),更是把nutch同其他開(kāi)源搜索引擎的差距拉開(kāi)。

          ???nutch的官方網(wǎng)站:http://lucene.apache.org/nutch/
          ???nutch的入門(mén)文章:http://lucene.apache.org/nutch/tutorial8.html

          ???以下詳細(xì)的介紹一下nutch0.8的安裝方法:

          一、環(huán)境:
          ??????1.操作系統(tǒng):windowsXp,windows2000+
          ??????2.javaVM:java1.5.x,設(shè)置JAVA_HOME到環(huán)境變量
          ??????3.cygwin,當(dāng)然這個(gè)不是必需的,只是nutch提供的腳本只能在shell環(huán)境下使用,所以使用cygwin來(lái)虛擬shell命令。
          ??????4.nutch版本:0.8
          ??????5.tomcat:5.0

          二、cygwin的安裝:

          ??????cygwin的安裝在NutchWindows中安裝之細(xì)解一文中有較為詳細(xì)的介紹,此處不再介紹安裝步驟,只介紹安裝后需要如何判斷是否能夠使用:在cygwin的安裝目錄下,查找x:\cygwin\cygwin\bin\sh.exe,存在此命令即可使用。
          ??????cygwin在刪除后會(huì)發(fā)現(xiàn)無(wú)法再次成功安裝的問(wèn)題,可以通過(guò)注冊(cè)表內(nèi)的查找功能,刪除所有包含cygwin內(nèi)容的鍵值即可。

          三、nutch的安裝和配置:

          ??????1。從http://lucene.apache.org/nutch/release/下載0.8或更高的版本,解壓縮后,放置到cygwin的根目錄下,如圖:

          ?

          ?????圖中可以看到nutch目錄在cygwin的根目錄下。
          ?????
          ??? 2。在nutch/bin下,建立urls目錄,然后建立一個(gè)url.txt文件,在url.txt文件內(nèi)寫(xiě)入一個(gè)希望爬行的url,例如:www.sina.com.cn,目錄結(jié)構(gòu)如圖:

          ?

          ?????
          ??????3。打開(kāi)nutch\conf\crawl-urlfilter.txt文件,把MY.DOMAIN.NAME字符替換為url.txt內(nèi)的url的域名,其實(shí)更簡(jiǎn)單點(diǎn),直接刪除MY.DOMAIN.NAME這幾個(gè)字就可以了,也就是說(shuō),只保存+^http://([a-z0-9]*\.)*這幾個(gè)字就可以了,表示所有http的網(wǎng)站都同意爬行。

          ??????4 。打開(kāi)nutch\conf\conf/nutch-site.xml文件,在<configuration></configuration>內(nèi)插入一下內(nèi)容:

          <property>

          ? <name>http.agent.name</name>

          ? <value></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></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></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></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>

          ?????把<name>XXX</name>之間的內(nèi)容替換為其他字符,當(dāng)然就算是不替換也無(wú)所謂,這里的設(shè)置,是因?yàn)閚utch遵守了robots協(xié)議,在獲取response時(shí),把自己的相關(guān)信息提交給被爬行的網(wǎng)站,以供識(shí)別。

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

          ????四、執(zhí)行nutch

          ???由于配置nutch采用的是單獨(dú)網(wǎng)站的配置方式,所以執(zhí)行上我們也采用的是單網(wǎng)查詢,全網(wǎng)查詢?cè)谝院蟮膬?nèi)容中介紹。

          ???先看一看nutch給出的命令:nutch crawl urls -dir crawl -depth 3 -topN 50
          ???crawl:通知nutch.jar,執(zhí)行crawl的main方法。
          ???urls:存放需要爬行的url.txt文件的目錄,注意,這個(gè)名字需要和你的文件夾目錄相同,如果你的文件夾為search,那這里也應(yīng)該改成search。
          ???-dir crawl:爬行后文件保存的位置,可以在nutch/bin目錄下找到。
          ???-depth 3:爬行次數(shù),或者成為深度,不過(guò)還是覺(jué)得次數(shù)更貼切,建議測(cè)試時(shí)改為1。
          ???-topN 50:一個(gè)網(wǎng)站保存的最大頁(yè)面數(shù)。


          ??????執(zhí)行命令的步驟:
          ??????1。進(jìn)入cygwin界面。
          ??????2。使用cd命令,進(jìn)入nutch\bin路徑下。
          ??????3。執(zhí)行:sh nutch crawl urls -dir crawl -depth 3 -topN 50

          ???具體的爬行日志可以在nutch/logs目錄下看到,注意查找“INFO? fetcher.Fetcher - fetching http://XXXXXXX”這樣的內(nèi)容,這里是抓去過(guò)程日志。

          ???五、查詢搜索:
          ???nutch提供了類似google、baidu的網(wǎng)頁(yè)頁(yè)面,在nutch壓縮包下找到nutch-0.8.war文件,放到tomcat/webapps目錄下,修改webapps/nutch/WEB-INF/classes/nutch-site.xml文件內(nèi)容如下:

          <property>
          <name>searcher.dir</name>
          <value>E:\\software\\splider\\nutch\\nutch-0.8\\nutch-0.8\\crawl</value>

          </property>

          ???<value/>的內(nèi)容是剛才爬行后的crawl目錄位置,提供給客戶端來(lái)查詢。

            配置完成后,啟動(dòng)tomcat,輸入http://localhost:8080/nutch,輸入關(guān)鍵字,就會(huì)看到結(jié)果了,下圖是我抓去wap網(wǎng)站的測(cè)試結(jié)果:


          ???六、總結(jié):
          ???ntuch提供了一個(gè)高效、開(kāi)源、易操作的搜索引擎,內(nèi)部有許多細(xì)微之處都是值得借鑒的,例如采用了hadoop的分布式文件系統(tǒng),類似eclipse的插件技術(shù),apache的httpclient來(lái)訪問(wèn)網(wǎng)站,org.cyberneko.html得HtmlParse來(lái)解析頁(yè)面等等,在以后會(huì)逐個(gè)介紹。


          歡迎轉(zhuǎn)載,請(qǐng)注明出處!


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 克什克腾旗| 温州市| 科尔| 安吉县| 东宁县| 新巴尔虎左旗| 安国市| 横山县| 德安县| 崇州市| 天全县| 北川| 汉沽区| 中宁县| 尉犁县| 青海省| 蓬溪县| 中牟县| 乾安县| 高邑县| 宣汉县| 松阳县| 家居| 定陶县| 安平县| 剑河县| 都昌县| 柯坪县| 随州市| 赤峰市| 三原县| 南靖县| 灵台县| 富裕县| 平乐县| 凤翔县| 循化| 晋中市| 凉城县| 永和县| 绍兴市|