简体   繁体   English

Scrapy-每个项目使用不同的管道

[英]Scrapy - Different pipeline per Item

I'm new with scrapy and python so forgive my ignorance about this. 我是scrapy和python的新手,所以请原谅我对此的无知。

I need to store in a database two different type of items. 我需要在数据库中存储两种不同类型的项目。 For one of them I need to do some extra queries before I do the insert. 对于其中之一,我需要在插入之前做一些额外的查询。 Is it possible to have a different pipeline based on the Item? 是否有可能基于物料具有不同的管道? If not, how can I differentiate which item is which when the get to the pipeline? 如果不是,当到达管道时,如何区分哪个项目是哪个项目?

Basically you can discard the item that you don't wanna process in certain pipeline and vice ver sa. 基本上,您可以丢弃某些管道中不想处理的项目,反之亦然。 For example: 例如:

class ApplePipeLine(object):

    def process_item(self, item, spider):
        if not isinstance(item, Apple):
            return item
        # Do something with Apple
        return item


class OrangePipeLine(object):

    def process_item(self, item, spider):
        if not isinstance(item, Orange):
            return item
        # Do something with Orange
        return item

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM