nutch 配备crawl-urlfilter.txt,regex-urlfilter.txt和nutch-site.xml
1:解压缩的nutch后,到conf下面修改crawl-urlfilter.txt
# accept hosts in MY.DOMAIN.NAME
+^http://([a-z0-9]*\.)*apache.org/
+^http://([a-z0-9]*\.)*longtask.com/blog/
如果有需要可以继续添加相关的站点.
2:修改conf下面的nutch-site.xml文件,在<configuration>之间添加以下内容
<property>
<name>http.agent.name</name>
<value>longtask</value>
<description>HTTP ‘User-Agent’ request header. </description>
</property>
<property>
<name>http.agent.description</name>
<value>longtask</value>
<description>Further description of our bot- this text is used in the User-Agent header.
</description>
</property>
<property>
<name>http.agent.url</name>
<value>http://www.longtask.com/blog/</value>
<description>A URL to advertise in the User-Agent header.
</description>
</property>
<property>
<name>http.agent.email</name>
<value>longtask@gmail.com</value>
<description>An email address to advertise in the HTTP ‘From’ reques header and User-Agent header.
</description>
</property>
3:回到到nutch的根目录,进入bin目录,建一个urls的文件夹,在文件夹中建一个nutch.txt的问题件,在文件中加入上面要鉴权的Url地址(抓取的网址(nutch.txt)经过(crawl-urlfilters.xml)过滤后,如果没有内容会在log中报错:Stopping at depth=0 - no more URLs to fetch.)。这个地方用nutch1.0怎么都跑不过,只能换回0.9的版本了。
三:解决搜索动态内容的问题:
需要注意在conf下面的2个文件:regex-urlfilter.txt,crawl-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
四:运行爬虫,抓取内容:
打开Cygwin,
在命令行窗口中输入: cd nutch的目录/bin
执行命令:
sh nutch crawl urls -dir e:/index -depth 3 -threads 4 -topN 50 >& ./log.txt
说明:
-dir dirnames 设置保存所抓取网页的目录.
-depth depth 表明抓取网页的层次深度
-delay delay 表明访问不同主机的延时,单位为“秒”
-threads threads 表明需要启动的线程数
-topN number 在每次迭代中限制爬行的头几个链接数,默认是Integer.MAX_VALUE
运行结束后,查看log.txt日志,会有爬虫检索网页的详细信息。
问题的解决:运行的过程中报错:
Stopping at depth=0 - no more URLs to fetch.
No URLs to fetch - check your seed list and URL filters.
参考实例项目:http://www.lsoba.com