天天看點

Nutch-2.2.1----過濾抓取資料

轉載源位址:http://blog.csdn.net/skywalker_only/article/details/17562543

在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.
-[?*[email protected]=]

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

# accept anything else
+.
           

其中#表示注釋内容,-表示忽略,+表示包含。若待抓取的url比對該檔案中的一個模式,則根據該模式前面的加号或者減号來判斷該url是否抓取或者忽略,若url跟該檔案中定義的規則都不比對,則忽略該url。

繼續閱讀