分享

Nutch-2.2.1系列之七过滤抓取数据

tntzbzc 2014-11-17 21:24:52 发表于 连载型 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 1 23583
问题导读
1.在Nutch的conf目录中默认有哪些文件起作用?
2.Nutch的conf目录中文件各自的作用是什么?
3.Nutch的过滤器原理的是什么?
扩展:
4.文件如何配置过滤作用,比如定义了不抓取后缀为gif、exe的文件等?









在Nutch的conf目录中有automaton-urlfilter.txt、regex-urlfilter.txt、suffix-urlfilter.txt、prefix-urlfilter.txt、domain-urlfilter.txt几个文件用于实现过滤抓取数据,比如不抓取后缀为gif、exe的文件等,通过修改其中的值可以达到只抓取感兴趣的内容的目的,在一定程度上也有助于提高抓取速度。
在抓取过程中,这几个文件不是都起作用的,默认情况下只有regex-urlfilter.txt会达到过滤目的,这一点可以从Nutch-default.xml确认。在进行过滤规则的修改之前,先说明Nutch的过滤器原理。在Nutch中,过滤器是通过插件的方式实现的,插件在nutch-default.xml中定义,具体如下:

  1. <!-- plugin properties -->
  2. <property>
  3.   <name>plugin.folders</name>
  4.   <value>plugins</value>
  5. </property>
  6. <property>
  7.   <name>plugin.auto-activation</name>
  8.   <value>true</value>
  9. </property>
  10. <property>
  11.   <name>plugin.includes</name>
  12. <value>protocol-http|urlfilter-regex|parse-(html|tika)|index-(basic|anchor)|urlnormalizer-(pass|regex|basic)|scoring-opic</value>
  13. </property>
  14. <property>
  15.   <name>plugin.excludes</name>
  16.   <value></value>
  17. </property>
复制代码


  
其中plugin.folders定义了插件放置的位置,该值可以为绝对路径或者相对路径,若为相对路径则会在classpath中搜索,默认值为plugins,编译后的Nutch,会包含该文件夹。plugin.includes以正则表达式的方式定义了哪些插件将被包含在Nutch中,可以根据属性值查看plugins中的目录来确定默认值,比如urlfilter-regex,则说明默认情况下,过滤器插件使用的是urlfilter-regex,也即regex-urlfilter.txt文件。plugin.excludes属性以正则表达式的方式定义了哪些插件将被排除在Nutch之外定义了。
在了解了插件的定义后,具体看看过滤器分几种以及如何定义的。过滤器在nutch-default.xml中的定义如下:
  1. <!-- urlfilter plugin properties -->
  2. <property>
  3.   <name>urlfilter.domain.file</name>
  4.   <value>domain-urlfilter.txt</value>
  5. </property>
  6. <property>
  7.   <name>urlfilter.regex.file</name>
  8.   <value>regex-urlfilter.txt</value>
  9. </property>
  10. <property>
  11.   <name>urlfilter.automaton.file</name>
  12.   <value>automaton-urlfilter.txt</value>
  13. </property>
  14. <property>
  15.   <name>urlfilter.prefix.file</name>
  16.   <value>prefix-urlfilter.txt</value>
  17. </property>
  18. <property>
  19.   <name>urlfilter.suffix.file</name>
  20.   <value>suffix-urlfilter.txt</value>
  21. </property>
  22. <property>
  23.   <name>urlfilter.order</name>
  24.   <value></value>
  25. </property>
复制代码


通过上面的代码可知,过滤器可以分为5种,分别为:DomainURLFilter、RegexURLFilter、AutomatonURLFilter 、PrefixURLFilter、SuffixURLFilter,这5中过滤器的配置过滤规则的文件分别为:domain-urlfilter.txt、regex-urlfilter.txt、automaton-urlfilter.txt、prefix-urlfilter.txt、suffix-urlfilter.txt。属性urlfilter.order则定义了过滤器的应用顺序,所有过滤器都是与的关系。
了解了Nutch中是如何定义过滤器之后,再来看看具体的过滤规则文件,以regex-urlfilter.txt(默认情况下即按照该文件中的规则抓取数据)为例。该文件中定义的规则如下:

  1. # skip file: ftp: and mailto: urls
  2. -^(file|ftp|mailto):
  3. # skip image and other suffixes we can't yet parse
  4. # for a more extensive coverage use the urlfilter-suffix plugin
  5. -\.(gif|GIF|jpg|JPG|png|PNG|ico|ICO|css|CSS|sit|SIT|eps|EPS|wmf|WMF|zip|ZIP|ppt|PPT|mpg|MPG|xls|XLS|gz|GZ|rpm|RPM|tgz|TGZ|mov|MOV|exe|EXE|jpeg|JPEG|bmp|BMP|js|JS)$
  6. # skip URLs containing certain characters as probable queries, etc.
  7. -[?*!@=]
  8. # skip URLs with slash-delimited segment that repeats 3+ times, to break loops
  9. -.*(/[^/]+)/[^/]+\1/[^/]+\1/
  10. # accept anything else
  11. +.
复制代码



其中#表示注释内容,-表示忽略,+表示包含。若待抓取的url匹配该文件中的一个模式,则根据该模式前面的加号或者减号来判断该url是否抓取或者忽略,若url跟该文件中定义的规则都不匹配,则忽略该url。



相关文章推荐





Nutch-2.2.1系列之一Nutch简介||及jar包问题解决
http://www.aboutyun.com/thread-10048-1-1.html

Nutch-2.2.1系列之二编译部署Nutch及常见问题
http://www.aboutyun.com/thread-10049-1-1.html

Nutch-2.2.1系列之三Nutch配置文件||Nutch与Hbase结合使用时常见问题
http://www.aboutyun.com/thread-10050-1-1.html

Nutch-2.2.1系列之四Nutch抓取数据在HBase中的存储
http://www.aboutyun.com/thread-10051-1-1.html


Nutch-2.2.1学习之五以伪分布模式运行Nutch
http://www.aboutyun.com/thread-10078-1-1.html

Nutch-2.2.1系列之六Nutch与Solr的集成
http://www.aboutyun.com/thread-10079-1-1.html



Nutch-2.2.1系列之八Nutch过滤URL实践
http://www.aboutyun.com/thread-10081-1-1.html

已有(1)人评论

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条