nutch 学习 过滤器


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中定义,具体如下:

<!-- plugin properties -->
<property>
  <name>plugin.folders</name>
  <value>plugins</value>
</property>
<property>
  <name>plugin.auto-activation</name>
  <value>true</value>
</property>
<property>
  <name>plugin.includes</name>
 <value>protocol-http|urlfilter-regex|parse-(html|tika)|index-(basic|anchor)|urlnormalizer-(pass|regex|basic)|scoring-opic</value>
</property>
<property>
  <name>plugin.excludes</name>
  <value></value>
</property>

其中plugin.folders定义了插件放置的位置,该值可以为绝对路径或者相对路径,若为相对路径则会在classpath中搜索,默认值为plugins,编译后的Nutch,会包含该文件夹。plugin.includes以正则表达式的方式定义了哪些插件将被包含在Nutch中,可以根据属性值查看plugins中的目录来确定默认值,比如urlfilter-regex,则说明默认情况下,过滤器插件使用的是urlfilter-regex,也即regex-urlfilter.txt文件。plugin.excludes属性以正则表达式的方式定义了哪些插件将被排除在Nutch之外定义了。

在了解了插件的定义后,具体看看过滤器分几种以及如何定义的。过滤器在nutch-default.xml中的定义如下:

<!-- urlfilter plugin properties -->
<property>
  <name>urlfilter.domain.file</name>
  <value>domain-urlfilter.txt</value>
</property>
<property>
  <name>urlfilter.regex.file</name>
  <value>regex-urlfilter.txt</value>
</property>
<property>
  <name>urlfilter.automaton.file</name>
  <value>automaton-urlfilter.txt</value>
</property>
<property>
  <name>urlfilter.prefix.file</name>
  <value>prefix-urlfilter.txt</value>
</property>
<property>
  <name>urlfilter.suffix.file</name>
  <value>suffix-urlfilter.txt</value>
</property>
<property>
  <name>urlfilter.order</name>
  <value></value>
</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(默认情况下即按照该文件中的规则抓取数据)为例。该文件中定义的规则如下:

# skip file: ftp: and mailto: urls
-^(file|ftp|mailto):

# skip image and other suffixes we can't yet parse
# for a more extensive coverage use the urlfilter-suffix plugin
-\.(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)$

# skip URLs containing certain characters as probable queries, etc.
-[?*!@=]

# skip URLs with slash-delimited segment that repeats 3+ times, to break loops
-.*(/[^/]+)/[^/]+\1/[^/]+\1/

# accept anything else
+.

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值