迭代爬取时,报错 Filtered offsite request

用scrapy框架迭代爬取时报错
scrapy日志:

在 setting.py 文件中 设置 日志 记录等级

LOG_LEVEL= 'DEBUG'

LOG_FILE ='log.txt'

观察 scrapy 日志

2017-08-15 21:58:05 [scrapy.spidermiddlewares.offsite] DEBUG: Filtered offsite request to 'sou.zhaopin.com': <GET http://sou.zhaopin.com/jobs/searchresult.ashx?jl=%E4%B8%8A%E6%B5%B7&kw=python&sm=0&source=0&p=2>
2017-08-15 21:58:05 [scrapy.core.engine] INFO: Closing spider (finished)
2017-08-15 21:58:05 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 782,
 'downloader/request_count': 3,
 'downloader/request_method_count/GET': 3,
 'downloader/response_bytes': 58273,
 'downloader/response_count': 3,
 'downloader/response_status_count/200': 2,
 'downloader/response_status_count/302': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2017, 8, 15, 13, 58, 5, 915565),
 'item_scraped_count': 59,
 'log_count/DEBUG': 64,
 'log_count/INFO': 7,
 'memusage/max': 52699136,
 'memusage/startup': 52699136,
 'offsite/domains': 1,
 'offsite/filtered': 1,
 'request_depth_max': 1,
 'response_received_count': 2,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2017, 8, 15, 13, 58, 5, 98357)}
2017-08-15 21:58:05 [scrapy.core.engine] INFO: Spider closed (finished)

重要的是第一行,我开始做的时候没有意识到这竟然是一个错误,应该是被记录的一个错误提示,然后程序也就没有报错

2017-08-15 21:58:05 [scrapy.spidermiddlewares.offsite] DEBUG: Filtered offsite request to 'sou.zhaopin.com': <GET http://sou.zhaopin.com/jobs/searchresult.ashx?jl=%E4%B8%8A%E6%B5%B7&kw=python&sm=0&source=0&p=2>

DEBUG: Filtered offsite request to
因为 Request中请求的 URL 和 allowed_domains 中定义的域名冲突,所以将Request中请求的URL过滤掉了,无法请求

    name = 'zhilianspider'
    allowed_domains = ['http://sou.zhaopin.com']

    page = 1
    url = 'http://sou.zhaopin.com/jobs/searchresult.ashx?jl=%E4%B8%8A%E6%B5%B7&kw=python&sm=0&source=0&p='
    start_urls = [url+str(page)]

在 Request 请求参数中,设置 dont_filter = True ,Request 中请求的 URL 将不通过 allowed_domains 过滤。

if self.page <= 10:
            self.page +=1
            yield scrapy.Request(self.url+str(self.page),callback=self.parse,dont_filter = True)

由于关掉了allowed_domains 过滤,所以要将yield 写在判断条件呢,开始我写在了外面程序一直迭代,停不下来了,尴尬。
之前都是写在if同级下的,那时候还没有关掉过滤所以没问题

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • scrapy学习笔记(有示例版) 我的博客 scrapy学习笔记1.使用scrapy1.1创建工程1.2创建爬虫模...
    陈思煜阅读 12,780评论 4 46
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,937评论 18 139
  • 好物告诉你,中国亚马逊的该买与不该买。 首先要问一句,经过双十一的洗礼,各位的钱包还健在不? 错过了折扣不要紧,没...
    纳谷nakko阅读 1,211评论 3 9
  • 紫花恋雨腻清晨,黄杏惜枝慕叶深。 风过闲庭何处诉,铃音阵阵荡春心。 作者王永豪
    王永豪阅读 361评论 0 6
  • “该你说了”“我说一”“对!一一一一一一一一一一一一一一一一一一一一...
    孤岛拜阅读 333评论 0 0