我创建了一个包含两个图像的scrapy项目。这些图像需要在我的项目管道中进行后处理。但是,这两种图像都有不同的后处理需求。
我的图片管道目前看起来像这样:
import scrapy
from scrapy.contrib.pipeline.images import ImagesPipeline
from scrapy.exceptions import DropItem
class CustomImagePipeline(ImagesPipeline):
def get_media_requests(self, item, info):
yield scrapy.Request(item['image_1'][0])
yield scrapy.Request(item['image_2'][0])
def item_completed(self, results, item, info):
# if image_1 is completed do one thing.
# if image_2 is completed to another thing.
return item
如何知道item_completed
回调方法中哪些图片已完成?
我可以以某种方式在results
字典中附加一个字段吗?
答案 0 :(得分:1)
根据the docs,您可以确定哪个图片是哪个 -
def item_completed(self, results, item, info):
for result in [x for ok, x in results if ok]:
# Invoke relevant post-processing based on result['url']
return item