我已经为我的垃圾项目写了我自己的图像管道。从我的google上我得到了关于如何设置管道的不同信息设置.py. 在
假设管道是MyImagesPipeline,它存在于管道.py其中包括:
class MyImagesPipeline(ImagesPipeline):
def get_media_requests(self, item, info):
for image_url in item['image_urls']:
yield scrapy.Request(image_url)
def item_completed(self, results, item, info):
some processing...
return item
在我的设置.py公司名称:
^{pr2}$我有两个管道,因为如果我单独放入MyImagesPipeline,item_completed会被调用,但是没有任何图像,我会得到一个KeyError,因为字段“images”不在那里。然而,在两个中间件的设置,我得到了同一个图像的多个副本。在
有人能告诉我这件事吗?在
编辑:
spider代码很长,因为我在其中进行了大量的信息处理,但我认为以下是相关部分(parse的回调):
def parse_data(self, response):
img_urls = response.css('.product-image').xpath('.//img/@src').extract()
img_url = img_urls[0]
item['image_urls'] = [img_url,]
yield item
两个图像管道都在处理项目中的
images_urls
字段,这就是为什么要两次获取它们的图像。在我将尝试使用单个管道并修复其中遇到的任何错误,以获得一个独立的组件来处理整个图像处理。特别是,您必须更好地处理来自
ImagesPipeline
的继承。在关于KeyError,} field in the items ,如果您重写它,它将在您需要时不可用。在
ImagesPipeline.item_completed
方法is in charge of updating the ^{要在管道中修复此问题,可以按如下方式进行更新: 在
我建议检查
ImagesPipeline
的代码(在Scrapy 1.0中它被放在scrapy/pipelines/images.py中,或者在以前的版本中放在scrapy/contrib/pipeline/images.py中,但是代码实际上是相同的)以完全理解它内部的情况。在相关问题 更多 >
编程相关推荐