栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在一个Scrapy项目中为不同的Spider使用不同的管道

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在一个Scrapy项目中为不同的Spider使用不同的管道

在Pablo Hoffman的解决方案的基础上,您可以在

process_item
Pipeline对象的方法上使用以下装饰器,以便它检查
pipeline
您的Spider的属性是否应执行。例如:

def check_spider_pipeline(process_item_method):    @functools.wraps(process_item_method)    def wrapper(self, item, spider):        # message template for debugging        msg = '%%s %s pipeline step' % (self.__class__.__name__,)        # if class is in the spider's pipeline, then use the        # process_item method normally.        if self.__class__ in spider.pipeline: spider.log(msg % 'executing', level=log.DEBUG) return process_item_method(self, item, spider)        # otherwise, just return the untouched item (skip this step in        # the pipeline)        else: spider.log(msg % 'skipping', level=log.DEBUG) return item    return wrapper

为了使此装饰器正常工作,蜘蛛程序必须具有管道属性,其中包含要用于处理项目的管道对象的容器,例如:

class MySpider(baseSpider):    pipeline = set([        pipelines.Save,        pipelines.Validate,    ])    def parse(self, response):        # insert scrapy goodness here        return item

然后在一个

pipelines.py
文件中:

class Save(object):    @check_spider_pipeline    def process_item(self, item, spider):        # do saving here        return itemclass Validate(object):    @check_spider_pipeline    def process_item(self, item, spider):        # do validating here        return item

所有Pipeline对象仍应在ITEM_PIPELINES中的设置中进行定义(以正确的顺序进行更改-这样很好,以便可以在Spider上指定顺序)。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/641017.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号