我已经为我的scrapy项目编写了自己的ImagePipeline.从我的谷歌搜索我得到有关如何在settings.py中设置pipline的不同信息.
假设管道是MyImagesPipeline,它存在于pipelines.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
Run Code Online (Sandbox Code Playgroud)
在我的settings.py中:
ITEM_PIPELINES = {
'scrapy.contrib.pipeline.images.ImagesPipeline': 1,
'myproject.pipelines.MyImagesPipeline': 100,
}
Run Code Online (Sandbox Code Playgroud)
我有两个管道,因为如果我单独放入MyImagesPipeline,则调用item_completed但没有任何图像,我得到一个KeyError,因为字段'images'不存在.但是,如果设置中的两个中间件,我将获得同一图像的多个副本.
有人可以请教我这个吗?
编辑:
蜘蛛代码很长,因为我在其中进行了大量的信息处理,但我认为可能是相关部分(解析回调):
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
Run Code Online (Sandbox Code Playgroud)
小智 1
两个图像管道都在处理images_urls您的项目中的字段,这就是您两次获取其图像的原因。
我会尝试坚持使用单个管道并修复您在其中遇到的任何错误,以获得处理整个图像处理的独立组件。特别是,您必须更好地处理来自的继承ImagesPipeline才能做到这一点。
关于 KeyError,该ImagesPipeline.item_completed方法负责更新imagesitems 中的字段,如果您覆盖它,那么当您需要它时它将不可用。
要修复管道中的问题,您可以像这样更新它:
class MyImagesPipeline(ImagesPipeline):
...
def item_completed(self, results, item, info):
item = super(MyImagesPipeline, self).item_completed(results, item, info)
some processing...
return item
Run Code Online (Sandbox Code Playgroud)
我建议检查ImagesPipeline的代码(它位于Scrapy 1.0 中的scrapy/pipelines/images.py或以前版本中的scrapy/contrib/pipeline/images.py中,但代码实际上是相同的)以完全了解其中发生的情况。
| 归档时间: |
|
| 查看次数: |
544 次 |
| 最近记录: |