Scrapy 如何在pipelines中获取爬虫名?

新手上路,请多包涵

需要在Pipeline获取到当前运行的爬虫name

spider.py

class Spider(scrapy.Spider):
    name = "xxxx"

pipelines.py

class spiderPipeline(object):
    def __init__(self):
        self.spidername = **?????**这里如何拿到当前爬虫name xxxxx?
阅读 4.2k
1 个回答

spider.py

import scrapy


class QuotesSpider(scrapy.Spider):
    name = "quotes"

pipelines.py

from scrapy.crawler import _get_spider_loader


class TutorialPipeline(object):

    def __init__(self, settings):
        for name in _get_spider_loader(settings).list():
            self.spidername = name

    @classmethod
    def from_crawler(cls, crawler):
        return cls(crawler.settings)

    def process_item(self, item, spider):
        return item

run
scrapy crawl quotes

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进