scrapy之Pipeline

官方文档:https://docs.scrapy.org/en/latest/topics/item-pipeline.html

  激活pipeline,需要在settings里配置,然而这里配置的pipeline会作用于所有的spider。加入项目中有很多spider在运行。item pipeline的处理就会很麻烦,你可以通过process_item(self,item,spider)中的spider参数来判断是来自哪个爬虫,但是这种方法很冗余。更好的做法是配置spider类中的custom_settings属性。为每一个spider配置不同的pipeline。示例如下:

  同时,这里你也会看到custom_settings的用法和用处。

class XiaohuaSpider(scrapy.Spider):
    name = 'xiaohua'
    custom_settings = {
        'ITEM_PIPELINES ':{
            'TB.pipelines.TBMongoPipeline':300,
        }
    }

 

一  method

  1 process_item(self,item,spider)

  This method is called for every item pipeline component

  2 open_spider(self,spider)

  This method is called when the spider is opened.

  3 close_spider(self,spider)

  4 from_crawler(cls,crawler)

  It must return a new instance of the pipeline

二 Item Pipeline example

  1 write items to mongodb

import pymongo

class MongoPipeline(object):

    collection_name = 'scrapy_items'

    def __init__(self, mongo_uri, mongo_db):
        self.mongo_uri = mongo_uri
        self.mongo_db = mongo_db

    @classmethod
    def from_crawler(cls, crawler):
        return cls(
            mongo_uri=crawler.settings.get('MONGO_URI'),
            mongo_db=crawler.settings.get('MONGO_DATABASE', 'items')
        )

    def open_spider(self, spider):
        self.client = pymongo.MongoClient(self.mongo_uri)
        self.db = self.client[self.mongo_db]

    def close_spider(self, spider):
        self.client.close()

    def process_item(self, item, spider):
        self.db[self.collection_name].insert_one(dict(item))
        return item

  2 duplicates filter

from scrapy.exceptions import DropItem

class DuplicatesPipeline(object):

    def __init__(self):
        self.ids_seen = set()

    def process_item(self, item, spider):
        if item['id'] in self.ids_seen:
            raise DropItem("Duplicate item found: %s" % item)
        else:
            self.ids_seen.add(item['id'])
            return item

 

  

转载于:https://www.cnblogs.com/654321cc/p/8877079.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值