Python 2.7 Scrapy Spider重试请求
有时,我会收到一个带有意外html的响应,并且不会提取所有项目字段,但是,如果我重试请求,它通常会返回预期的html 作为一个快速修复,我正在捕获spiderPython 2.7 Scrapy Spider重试请求,python-2.7,scrapy,scrapy-spider,Python 2.7,Scrapy,Scrapy Spider,有时,我会收到一个带有意外html的响应,并且不会提取所有项目字段,但是,如果我重试请求,它通常会返回预期的html 作为一个快速修复,我正在捕获spiderparse方法中的错误: # project/spiders/sample_spider.py class SampleSpider(Spider): [...] def parse(self, response): try: item = SampleItem()
parse
方法中的错误:
# project/spiders/sample_spider.py
class SampleSpider(Spider):
[...]
def parse(self, response):
try:
item = SampleItem()
item['sample_1'] = response.xpath('sample').extract()
item['product_count_2'] = response.xpath('sample').extract()[0]
yield item
except IndexError:
logger.debug('Retrying %(url)s', {'url': response.url})
yield Request(response.url, self.parse, dont_filter=True)
我遇到了一个类似的场景,但似乎这种类型的错误应该在项目管道中处理。。。关于实施此修复的最佳方法有何想法?为什么会失败、网络错误或是什么?@padraiccnningham,它返回200响应,但由于某些原因,它只是不返回预期的html。返回了什么html?请求的URL是搜索查询,它返回一个页面,说明我的搜索没有返回任何结果。该查询是有效的,因为重试请求会返回预期的html。