1.首先下載nutch的最新版本, http://apache.etoak.com/nutch/ 本文寫作時nutch的最新版本爲1.1。所以本文以下所述均針對nutch1.1。(注意筆者在下載時發現該版本的src包有問題。換成bin包後正常)
2.在eclipse中新建立一個Java Project. 名字自己定義(Nutch). 選擇"Create project from existing source",指向自己nutch-1.0的目錄.
3.單點finish完成。這時就把nutch的整個工程全部導入到了 eclipse中了。
4.此時還需將conf文件下的所有配置文件加入到classPath中。
右鍵conf――>Build PathàUse as Source Folder
5.修改配置文件
conf/nutch-site.xml <configuration>中加入以下內容。
<property>
<name>http.agent.name</name>
<value>test</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>test</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>www.163.com</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>[email protected]</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>
conf/nutch-default.xml
<property>
<name>plugin.folders</name>
<value>./src/plugin</value><!—修改部分-->
conf/crawl-urlfilter.txt
#accept hosts in MY.DOMAIN.NAME
+^http://([a-z0-9]*/.)*163.com/ ---寫入想抓取的網站正則表達式
6.在工程的根目錄下建立urls文件夾,其中新建一個url.txt文件,該文件中寫入想抓取的網站URL,如:http://www.163.com/
7.執行nutch的 crawl命令。
至此nutch導入eclipse中運行成功。
手動複製導入nutch代碼到eclipse中.(目錄結構更清楚)
通過以上方式雖然可以成功的將nutch導入eclipse中,但工程目錄結構極爲不適合,所以筆者又以手動的方式將nutch的源代碼導入eclipse中,使目錄更清晰。具體步驟如下:
1.在eclipse中新建立一個Java Project. 名字自己定義(Nutch). 選擇"Create New project in WorkSpace".點擊完成。
2.將解壓後的nutch目錄下的/src/java/中的代碼全部複製到新建工程中的src下。
將解壓後的nutch目錄下的lib、plugins、conf三個文件夾複製到新建工程的根目錄下(與src同級)
3.右鍵工程properties, 切換到"Libraries"選擇"Add Class Folder..." 按鈕,從列表中選擇"conf". 將 conf加入到classpath中。
4.修改配置文件
(1)conf/nutch-site.xml 同上.
(2)conf/nutch-default.xml
<property>
<name>plugin.folders</name>
<value> ./plugins</value><!—小心這裏路徑變了,筆者曾在此費了大半天時間-->
(3) conf/crawl-urlfilter.txt 同上。
(4)新建urls。同上
擔心內存不夠的話 在vm環境變量中加入 -Xms64m -Xmx512m
Xms (minimum ammount of RAM memory for running applications)
-Xms(運行程序最小的內存限制)
-Xmx (maximum)
(最大的)