笨鸟编程-零基础入门Pyhton教程

 找回密码
 立即注册

下载和处理文件和图像

发布者: 笨鸟自学网



自定义图像管道示例

下面是图像管道的完整示例,其方法如上图所示:

import scrapy
from itemadapter import ItemAdapter
from scrapy.exceptions import DropItem
from scrapy.pipelines.images import ImagesPipeline

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):
        image_paths = [x['path'] for ok, x in results if ok]
        if not image_paths:
            raise DropItem("Item contains no images")
        adapter = ItemAdapter(item)
        adapter['image_paths'] = image_paths
        return item

要启用自定义媒体管道组件,必须将其类导入路径添加到 ITEM_PIPELINES 设置,如以下示例中所示:

ITEM_PIPELINES = {
    'myproject.pipelines.MyImagesPipeline': 300
}
1234567
上一篇:调试内存泄漏下一篇:部署蜘蛛

Archiver|手机版|笨鸟自学网 ( 粤ICP备20019910号 )

GMT+8, 2024-12-22 13:16 , Processed in 0.028492 second(s), 17 queries .

© 2001-2020

返回顶部