codecamp

scrapy 2.3 自定义图像管道示例

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

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
}
scrapy 2.3 扩展媒体管道
scrapy 2.3 AutoThrottle扩展
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

scrapy 2.3 链接提取器

scrapy 2.3 统计数据集合

scrapy 2.3 发送电子邮件

scrapy 2.3 蜘蛛合约

scrapy 2.3 如何部署蜘蛛

scrapy 2.3 AutoThrottle扩展

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }