我有一个工作蜘蛛抓取图像 URL 并将它们放在 scrapy.Item 的 image_urls 字段中。我有一个继承自 ImagesPipeline 的自定义管道。当特定 URL 返回非 200 http 响应代码时(例如 401 错误)。例如,在日志文件中,我发现
WARNING:scrapy.pipelines.files:File (code: 404): Error downloading file from <GET http://a.espncdn.com/combiner/i%3Fimg%3D/i/headshots/tennis/players/full/425.png> referred in <None>
WARNING:scrapy.pipelines.files:File (code: 307): Error downloading file from <GET http://www.fansshare.com/photos/rogerfederer/federer-roger-federer-406468306.jpg> referred in <None>
但是,我无法在函数的自定义图像管道中捕获错误代码404、307item_completed()
等:
def item_completed(self, results, item, info):
image_paths = []
for download_status, x in results:
if download_status:
image_paths.append(x['path'])
item['images'] = image_paths # update item image path
item['result_download_status'] = 1
else:
item['result_download_status'] = 0
#x.printDetailedTraceback()
logging.info(repr(x)) # x is a twisted failure object
return item
在files.pymedia_downloaded()
的函数内部挖掘 scrapy 源代码,我发现对于非 200 响应代码,会记录一个警告(解释上述 WARNING 行),然后引发 a。FileException
if response.status != 200:
logger.warning(
'File (code: %(status)s): Error downloading file from '
'%(request)s referred in <%(referer)s>',
{'status': response.status,
'request': request, 'referer': referer},
extra={'spider': info.spider}
)
raise FileException('download-error')
我还如何访问此响应代码,以便我可以在 item_completed() 函数的管道中处理它?