【scrapy基础】scrapy项目下的多个爬虫如何在Pipeline中进行单独处理

项目场景:

项目场景:采集GitHub数据时需要对stars、watching、forks等用户信息进行处理,item需要在Pipeline中进行不同的处理。


问题描述:

最初使用标识符的方式解决这个问题,将不同的标识符存入item,然后在Pipeline进行判断处理。

github_commit.py

if item["user_name"] and item["content"]:
    item["pipeline_flag"] = "github_commit"
    yield item

github_main.py

if item["author"] and item["project_name"]:
    item["pipeline_flag"] = "github_main"
    yield item

pipelines.py

if item["pipeline_flag"] == "github_commit":
	logger.info("处理提交信息item")
elif item["pipeline_flag"] == "github_main":
	logger.info("处理项目信息item")

解决方案:

使用spider名称进行区分

if spider.name == "github_commit":
	logger.info("处理提交信息item")
elif spider.name == "github_main":
	logger.info("处理项目信息item")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值