1. 如何绕过目标站点的robots.txt限制
多数站点都是只允许百度、google等搜索引擎抓取的,所以会在robots.txt里限制其他爬虫。

nutch自然是会遵循robots协议的,但是我们可以通过修改nutch源码来绕过限制。

相关代码位于(nutch版本1.5.1,其他版本未测试):

org.apache.nutch.fetcher.Fetcher的run方法.

找到以下几行代码并注释掉就OK了。

  1. if (!rules.isAllowed(fit.u)) {
  2. // unblock
  3. fetchQueues.finishFetchItem(fit, true);
  4. if (LOG.isDebugEnabled()) {
  5. LOG.debug("Denied by robots.txt: " + fit.url);
  6. }
  7. output(fit.url, fit.datum, null, ProtocolStatus.STATUS_ROBOTS_DENIED, CrawlDatum.STATUS_FETCH_GONE);
  8. reporter.incrCounter("FetcherStatus", "robots_denied", 1);
  9. continue;
  10. }

2. url掉转导致html parse不成功的问题

在抓取百度百科的景点数据时,发现部分页面不会走html parse部分的逻辑,而我的plugin是基于HtmlParserFilter扩展点的,因而没有生效。

后来发现请求部分页面的链接返回的http状态为301,跳转之后才会到真正页面,而nutch默认是不会抓取跳转后的页面的.这时需要修改
nutch-site.xml,加入以下配置即可,nutch-default.xml里的默认值是0,我们这里改成一个大于0的值,nutch就会继续
抓取跳转后的页面了。

  1. <property>
  2. <name>http.redirect.max</name>
  3. <value>2</value>
  4. <description>The maximum number of redirects the fetcher will follow when
  5. trying to fetch a page. If set to negative or 0, fetcher won't immediately
  6. follow redirected URLs, instead it will record them for later fetching.
  7. </description>
  8. </property>

3. 抽取的过程中发现某些属性老是抽不到,而在不使用nutch抓取的情况下是能抽到的,进而怀疑nutch抓取的页面不全。于是去google了一下"nutch content limit",发现nutch有这么一个配置项:

  1. <property>
  2. <name>http.content.limit</name>
  3. <value>65536</value>
  4. <description>The length limit for downloaded content using the http://
  5. protocol, in bytes. If this value is nonnegative (>=0), content longer
  6. than it will be truncated; otherwise, no truncation at all. Do not
  7. confuse this setting with the file.content.limit setting.
  8. </description>
  9. </property>

用来限制抓取内容的大小,放大10倍后,问题解决。

需要注意的是nutch还有一个很容易混淆的配置项:

  1. <property>
  2. <name>file.content.limit</name>
  3. <value>65536</value>
  4. <description>The length limit for downloaded content using the file://
  5. protocol, in bytes. If this value is nonnegative (>=0), content longer
  6. than it will be truncated; otherwise, no truncation at all. Do not
  7. confuse this setting with the http.content.limit setting.
  8. </description>
  9. </property>

两个配置用于的协议不同,前者是http协议,后者是file协议,我一开始就配置错了,折腾了半天。。。

PS:最后推荐两篇介绍nutch的文章,在官方文档不那么给力的情况下,这两篇文章给了我不小的帮助,感谢下作者。

http://www.atlantbh.com/apache-nutch-overview/ 对nutch的整体流程做了介绍

http://www.atlantbh.com/precise-data-extraction-with-apache-nutch/ 用实际例子介绍了nutch plugin的开发和部署

最新文章

  1. JSON.stringify()和JSON.parse()
  2. 没听说过这些,就不要说你懂并发了,three。
  3. The requested operation has failed apache
  4. 0-Spark高级数据分析-读书笔记
  5. QT 信号与槽connect
  6. 登录时的&quot;记住我&quot;
  7. checkbox将选中的数据提交到后台
  8. 在CentOS6.5上安装Tomcat7
  9. SecureCRT学习之道:SecureCRT经常使用快捷键设置与字体设置方法
  10. GCD简介
  11. Vue.js-01:第一章 - 一些基础概念
  12. yii2 使用指定数据库执行createCommand
  13. C# 操作Word 中的OLE——插入、编辑、读取 OLE
  14. requirejs官网
  15. Cgroups 与 Systemd
  16. docker学习(一)
  17. Linux驱动之输入子系统简析
  18. 程序性能调优工具之gprob
  19. linux查看系统的硬件信息【转】
  20. ChibiOS/RT 2.6.9 CAN Low Level Driver for STM32

热门文章

  1. Android Studio 使用socks代理
  2. J.U.C 整体认识
  3. django之模型
  4. go run helper
  5. auto_ptr 浅析(转)
  6. Git报错的解决方案汇总
  7. canvas动画--demo
  8. JVM配置参数
  9. HTML 页面中的 SVG
  10. MySqlDBHelper