项目场景:
项目场景:采集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")