首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Scrapy:多个spider时指定pipeline

Scrapy:多个spider时指定pipeline

作者头像
新码农
修改2020-12-25 11:32:56
1.9K0
修改2020-12-25 11:32:56
举报
文章被收录于专栏:新码农博客新码农博客

导读

Scrapy存在多个爬虫的时候如何指定对应的管道呢?

1、在 pipeline 里判断爬虫

  • settings.py
ITEM_PIPELINES = {
    "xxxx.pipelines.MyPipeline": 300,
}
  • OneSpider.py
class OneSpider(scrapy.spiders.Spider):
    name = "one"
  • TwoSpider.py
class TwoSpider(scrapy.spiders.Spider):
    name = "two"
  • pipelines.py
class MyPipeline(object):
    def process_item(self, item, spider):

        if spider.name == "one":
            print("one")
        elif spider.name == "two":
            print("two")

        return item

2、在爬虫里设置 pipeline(scrapy版本必须是1.1以上)

  • settings.py
ITEM_PIPELINES = {
    "xxxx.pipelines.OneSpiderPipeline": 300,
    "xxxx.pipelines.TwoSpiderPipeline": 400,
}
  • OneSpider.py
class OneSpider(scrapy.Spider):
    name = "one"
    custom_settings = {
        "ITEM_PIPELINES": {"xxxx.pipelines.OneSpiderPipeline": 300},
    }
  • TwoSpider.py
class TwoSpider(scrapy.Spider):
    name = "two"
    custom_settings = {
        "ITEM_PIPELINES": {"xxxx.pipelines.TwoSpiderPipeline": 400},
    }
  • pipelines.py
class OneSpiderPipeline(object):
    def process_item(self, item, spider):
        print("one")

        return item


class TwoSpiderPipeline(object):
    def process_item(self, item, spider):
        print("two")

        return item
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020年4月16日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 导读
    • 1、在 pipeline 里判断爬虫
      • 2、在爬虫里设置 pipeline(scrapy版本必须是1.1以上)
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档