nutch搜索引擎的搭建以及配置

最近公司需要搭建一個搜索引擎,於是就發現了apache旗下的這個nutch,也看了不少的文章,就在本地搭建了一個進行測試,發現局域網抓取還是比較好的,但是在互聯網抓取還是有點問題,像百度、谷歌這些站點的頁面基本就抓不到上信息,不知道是配置問題還是其他問題,希望有知道的朋友聯繫我,謝謝.

 

nutch搜索引擎的搭建以及配置

實驗環境:

   vmware 6.0

   redhat 5.1

軟件環境

   apache-tomcat-6.0.29.tar.gz

   nutch-1.0.tar.gz

   jdk-6u21-linux-i586.bin

 

nutchg簡介

 

Nutch的爬蟲抓取網頁有兩種方式,一種方式是Intranet Crawling,針對的是企業內部網或少量網站,使用的是crawl命令;另一種方式是Whole-web crawling,針對的是整個互聯網,使用injectgeneratefetchupdatedb等更底層的命令.本文檔介紹Intranet Crawling的基本使用方法.

安裝jdk

 # cp jdk-6u21-linux-i586.bin  /usr/java

 # cd  /usr/java

 # chmod +x  jdk-6u21-linux-i586.bin

 # ./ jdk-6u21-linux-i586

 

# vi /etc/profile      //添加如下的java環境變量

 

JAVA_HOME=/usr/java/jdk1.6.0_21

export JAVA_HOME

PATH=$JAVA_HOME/bin:$PATH

export PATH

CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH

export CLASSPATH

 

# source /etc/profile     //java環境變量立即生效

# java -version           //測試java環境是否正常,返回版本信息,就表示jdk安裝沒有問題

 

安裝tomcat

#  tar zxvf apache-tomcat-6.0.29.tar.gz  -C /usr/local

#  cd /usr/local/

#  mv  apache-tomcat-6.0.29   tomcat

安裝nutch

#  tar zxvf nutch-1.0.tar.gz  -C /usr/local

#  cd /usr/local

#  mv nutch-1.0 nutch

#  cd nutch

設置Nutch的環境變量

增加NUTCH_JAVA_HOME變量,並將其值設爲JDK的安裝目錄

NUTCH_JAVA_HOME=/usr/java/jdk1.6.0_21

export NUTCH_JAVA_HOME


Nutch抓取網站頁面前的準備工作

 

Nutch的安裝目錄中建立一個名爲url.txt的文本文件,文件中寫入要抓取網站的頂級網址,即要抓取的起始頁.

這裏寫入國內比較有名的站點 

 

編輯conf/crawl-urlfilter.txt文件,修改MY.DOMAIN.NAME部分:
   
  # accept hosts in MY.DOMAIN.NAME
  +^http://([a-z0-9]*\.)*com/
    +^http://([a-z0-9]*\.)*cn/
    +^http://([a-z0-9]*\.)*net/

 

解決搜索動態內容的問題
  需要注意在conf下面的2個文件:regex-urlfilter.txtcrawl-urlfilter.txt
 # skip URLs containing certain characters as probable queries, etc.
   
-[?*!@=]
   
這段意思是跳過在連接中存在? * ! @ = 的頁面,因爲默認是跳過所以,在動態頁中存在?一般

按照默認的是不能抓取到的.可以在上面2個文件中都修改成:

# skip URLs containing certain characters as probable queries, etc.
  
  # -[?*!@=]    //前面加上註釋.

另外增加允許的一行
    # accept URLs containing certain characters as probable queries, etc.
    
+[?=&]
   
意思是抓取時候允許抓取連接中帶 ? = & 這三個符號的連接
   注意:兩個文件都需要修改,因爲NUTCH加載規則的順序是crawl-urlfilter.txt->

     regex-urlfilter.txt

 

編輯conf/nutch-site.xml文件,configuration中間加入一下內容

 

    <property>

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

        <value>sxit  nutch agent</value>

    </property>

    <property>

        <name>http.agent.version</name>

        <value>1.0</value>

    </property>

運行Crawl命令抓取網站內容

/usr/local/nutch/bin/nutch crawl /usr/local/nutch/url.txt  -dir /usr/local/nutch/sxit  -depth 3  -threads 4  >&  /usr/loca/nutch/crawl.log

等待大約一段時間後,程序運行結束.會發現在nutch目錄下被創建了一個名爲sxit的文件夾,同時還生成一個名爲crawl.log的日誌文件.利用這一日誌文件,我們可以分析可能遇到的任何錯誤.另外,在上述命令的參數中,dir指定抓取內容所存放的目錄,depth表示以要抓取網站頂級網址爲起點的爬行深度,threads指定併發的線程數.

 

使用Tomcat進行搜索測試

 

nutch目錄的nutch-1.0.war複製到tomcat\webapps,這裏需要啓動下tomcat,然後就在webapps下面生成一個nutch-1.0的文件夾,打開 nutch-1.0\WEB-INF\classes下的nutch-site.xml文件,

//由於這裏是最新的版本,原來這個配置文件的內容都刪掉,添加如下的內容

 

  <?xml version="1.0"?>
  <?xml-stylesheet type="text/xsl" href="nutch-conf.xsl"?>

  <!-- Put site-specific property overrides in this file. -->

<nutch-conf>
   <property>
    <name>searcher.dir</name>
    <value>/usr/local/nutch/sxit</value>   //這裏爲剛纔
抓取內容所存放的目錄
   </property>
  </nutch-conf>
  

啓動Tomcat,打開瀏覽器在地址欄中輸入:http://IP:8080,/nutch-1.0,就可以看到搜索頁面了.、
在文本框中輸入關鍵字,就可以進行搜索了.不過用戶在使用時會發現,對於英文單詞的搜索一切正常,而當要搜索中文詞語時會出現亂碼.其實這個問題是Tomcat設置的問題,解決辦法是修改tomcat\conf下的server.xml文件,將其中的Connector部分改成如下形式即可:
   
  <Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" redirectPort="8443" acceptCount="100"
               connectionTimeout="20000" disableUploadTimeout="true" 
               URIEncoding="UTF-8" useBodyEncodingForURI="true" />

nutch的分頁功能

# cd /usr/local/apache-tomcat-6.0.29/ webapps/nutch-1.0

 

# vi search.jsp

 

查找int hitsPerSite =後面的值改成0,

然後在這個jsp文件的末尾增加如下的代碼:

 

  <table align="center">
            <tr>
            <td>
                    <%
                        if (start >= hitsPerPage) // more hits to show
                        {
                    %>
                    <form name="pre" action="../search.jsp" method="get">
                        <input type="hidden" name="query" value="<%=htmlQueryString%>">
                        <input type="hidden" name="lang" value="<%=queryLang%>">
                        <input type="hidden" name="start" value="<%=start - hitsPerPage%>">
                        <input type="hidden" name="hitsPerPage" value="<%=hitsPerPage%>">
                        <input type="hidden" name="hitsPerSite" value="<%=hitsPerSite%>">
                        <input type="hidden" name="clustering" value="<%=clustering%>">
                        <input type="submit" value="
上一頁">
                        <%} %>
                    </form>
                    <%
                    int startnum=1;//
頁面中最前面的頁碼編號,我設定(滿足)共10頁,當頁爲第6
                    if((int)(start/hitsPerPage)>=5)
                    startnum=(int)(start/hitsPerPage)-4;
                    for(int i=hitsPerPage*(startnum-1),j=0;i<=hits.getTotal()&&j<=10;)
                    {
                     %>
                     <td>
                     <form name="next" action="../search.jsp" method="get">
                        <input type="hidden" name="query" value="<%=htmlQueryString%>">
                        <input type="hidden" name="lang" value="<%=queryLang%>">
                        <input type="hidden" name="start" value="<%=i%>">
                        <input type="hidden" name="hitsPerPage" value="<%=hitsPerPage%>">
                        <input type="hidden" name="hitsPerSite" value="<%=hitsPerSite%>">
                        <input type="hidden" name="clustering" value="<%=clustering%>">
                        <input type="submit" value="<%=i/hitsPerPage+1 %>">
                    </form>
                    </td>
                    <%
                    i=i+10;  //
這裏的10是分頁顯示頁面數
                    j++;
                    }
                     %>
                <td>
                    <%
         if ((hits.totalIsExact() && end < hits.getTotal()) // more hits to show
                                || (!hits.totalIsExact() && (hits.getLength() > start
                                + hitsPerPage))) {
                    %>
                   
                    <form name="next" action="../search.jsp" method="get">
                        <input type="hidden" name="query" value="<%=htmlQueryString%>">
                        <input type="hidden" name="lang" value="<%=queryLang%>">
                        <input type="hidden" name="start" value="<%=end%>">
                        <input type="hidden" name="hitsPerPage" value="<%=hitsPerPage%>">
                        <input type="hidden" name="hitsPerSite" value="<%=hitsPerSite%>">
                        <input type="hidden" name="clustering" value="<%=clustering%>">
                        <input type="submit" value="<i18n:message key="next"/>">//
下一頁
                    </form>
                    <%} %>
     </td>
 </tr>
</table>

由於每次都要進行增量抓取,就搞了一個增量抓取的腳本,代碼如下:

 

  1. #!/bin/sh     
  2.     
  3. depth=5    
  4. threads=5     
  5. RMARGS="-rf"    
  6. MVARGS="--verbose"       
  7. safe=yes     
  8.  
  9. NUTCH_HOME=/usr/local/nutch            
  10.  
  11. CATALINA_HOME=/usr/local/apache-tomcat-6.0.29  
  12.     
  13. if [ -z "$NUTCH_HOME" ]     
  14. then     
  15.   echo runbot: $0 could not find environment variable NUTCH_HOME     
  16.   echo runbot: NUTCH_HOME=$NUTCH_HOME has been set by the script      
  17. else    
  18.   echo runbot: $0 found environment variable NUTCH_HOME=$NUTCH_HOME      
  19. fi     
  20.        
  21. if [ -z "$CATALINA_HOME" ]     
  22. then     
  23.   echo runbot: $0 could not find environment variable NUTCH_HOME     
  24.   echo runbot: CATALINA_HOME=$CATALINA_HOME has been set by the script      
  25. else    
  26.   echo runbot: $0 found environment variable CATALINA_HOME=$CATALINA_HOME      
  27. fi     
  28.  
  29. if [ -n "$topN" ]     
  30. then     
  31.   topN="-topN $topN"    
  32. else    
  33.   topN=""    
  34. fi    
  35.  
  36. steps=8  
  37.  
  38. echo "----- Inject (Step 1 of $steps) -----"    
  39. $NUTCH_HOME/bin/nutch inject $NUTCH_HOME/sxit/crawldb $NUTCH_HOME/url.txt     
  40.     
  41.     
  42. echo "----- Generate, Fetch, Parse, Update (Step 2 o $steps) -----"    
  43. for((i=0; i <= $depth; i++))     
  44. do    
  45.   echo "--- Beginning crawl at depth `expr $i + 1` of $depth ---"    
  46.   $NUTCH_HOME/bin/nutch generate $NUTCH_HOME/sxit/crawldb  $NUTCH_HOME/sxit/segments   
  47.   if [ $? -ne 0 ]     
  48.   then     
  49.     echo "runbot: Stopping at depth $depth. No more URLs to fetcfh."    
  50.     break    
  51.   fi     
  52.   segment=`ls -d $NUTCH_HOME/sxit/segments/* | tail -1`    
  53.  
  54.   $NUTCH_HOME/bin/nutch fetch $segment -threads $threads     
  55.   if [ $? -ne 0 ]     
  56.   then     
  57.     echo "runbot: fetch $segment at depth `expr $i + 1` failed."    
  58.     echo "runbot: Deleting segment $segment."    
  59.     rm $RMARGS $segment     
  60.     continue    
  61.   fi     
  62.  
  63.   $NUTCH_HOME/bin/nutch updatedb $NUTCH_HOME/sxit/crawldb  $segment     
  64. done     
  65.     
  66. echo "----- Merge Segments (Step 3 of $steps) -----"       
  67. $NUTCH_HOME/bin/nutch mergesegs $NUTCH_HOME/sxit/MERGEDsegments $NUTCH_HOME/sxit/segments/*     
  68.        
  69. mv $MVARGS $NUTCH_HOME/sxit/segments $NUTCH_HOME/sxit/BACKUPsegments     
  70. mkdir $NUTCH_HOME/sxit/segments     
  71. mv $MVARGS $NUTCH_HOME/sxit/MERGEDsegments/* $NUTCH_HOME/sxit/segments     
  72. rm $RMARGS $NUTCH_HOME/sxit/MERGEDsegments     
  73.    
  74. echo "----- Invert Links (Step 4 of $steps) -----"    
  75. $NUTCH_HOME/bin/nutch invertlinks $NUTCH_HOME/sxit/linkdb $NUTCH_HOME/sxit/segments/*     
  76.      
  77. echo "----- Index (Step 5 of $steps) -----"    
  78. $NUTCH_HOME/bin/nutch index $NUTCH_HOME/sxit/NEWindexes $NUTCH_HOME/sxit/crawldb $NUTCH_HOME/sxit/linkdb $NUTCH_HOME/sxit/segments/*      
  79.  
  80. echo "----- Dedup (Step 6 of $steps) -----"    
  81. $NUTCH_HOME/bin/nutch dedup $NUTCH_HOME/sxit/NEWindexes     
  82.     
  83. echo "----- Merge Indexes (Step 7 of $steps) -----"    
  84. $NUTCH_HOME/bin/nutch merge $NUTCH_HOME/sxit/NEWindex $NUTCH_HOME/sxit/NEWindexes     
  85.       
  86. echo "----- Loading New Index (Step 8 of $steps) -----"    
  87. tom_pid=`ps aux |awk '/usr\/local\/apache-tomcat-6.0.29/ {print $2}'`  
  88. `kill -9 $tom_pid`  
  89.      
  90.      
  91. if [ "$safe" != "yes" ]     
  92. then     
  93.   rm $RMARGS $NUTCH_HOME/sxit/NEWindexes     
  94.   rm $RMARGS $NUTCH_HOME/sxit/index 
  95. else    
  96.   mv $MVARGS $NUTCH_HOME/sxit/NEWindexes $NUTCH_HOME/sxit/indexes  
  97.   mv $MVARGS $NUTCH_HOME/sxit/NEWindex   $NUTCH_HOME/sxit/index     
  98. fi   
  99.  
  100. ${CATALINA_HOME}/bin/startup.sh     
  101.     
  102. echo "runbot: FINISHED: Crawl completed!"    
  103. echo ""   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章