简体   繁体   English

是否可以从管道中为Scrapy中的特定Spider访问统计信息?

[英]Is it possible to access stats from pipeline for a particular Spider in Scrapy?

I am using Scrapy with several Spiders, and need custom json output, that would include some Spider stats (list of successful requests, list of errors, etc). 我正在将Scrapy与多个Spider配合使用,并且需要自定义json输出,其中将包含一些Spider统计信息(成功请求列表,错误列表等)。 I have made custom item pipeline, but I don't know how to access stats from there. 我已经制作了自定义项目管道,但是我不知道如何从那里访问统计信息。 This is my pipeline code so far: 到目前为止,这是我的管道代码:

class JsonWithEncodingPipeline(object):

    def open_spider(self, spider):
        self.file = codecs.open(spider.output_path, 'w', encoding='utf-8')

    def process_item(self, item, spider):
        line = json.dumps(dict(item), ensure_ascii=False, indent=2) + "\n"
        self.file.write(line)
        return item

    def spider_closed(self, spider):
        self.file.close()

You can access stats like this: 您可以像这样访问统计信息:

class MyPipeline:

    def __init__(self, stats):
        self.stats = stats

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM