Python Can';t从Scrapy.CrawlerProcess获取Scrapy统计信息

Python Can';t从Scrapy.CrawlerProcess获取Scrapy统计信息,python,web-scraping,scrapy,web-crawler,scrapy-spider,Python,Web Scraping,Scrapy,Web Crawler,Scrapy Spider,我正在运行另一个脚本中的scrapy Spider,我需要从Crawler中检索并保存到变量stats。我已经研究了文档和其他StackOverflow问题,但是我还没有解决这个问题 这是我运行爬网的脚本: import scrapy from scrapy.crawler import CrawlerProcess process = CrawlerProcess({}) process.crawl(spiders.MySpider) process.start() stats = Cr

我正在运行另一个脚本中的scrapy Spider,我需要从Crawler中检索并保存到变量stats。我已经研究了文档和其他StackOverflow问题,但是我还没有解决这个问题

这是我运行爬网的脚本:

import scrapy
from scrapy.crawler import CrawlerProcess


process = CrawlerProcess({})
process.crawl(spiders.MySpider)
process.start()

stats = CrawlerProcess.stats.getstats() # I need something like this
我希望统计数据包含这段数据(scrapy.statcollector):

我已经检查了CrawlerProcess,它返回deferred并在抓取过程完成后从其“crawlers”字段中删除爬虫

有办法解决这个问题吗

最好的, Peter

根据,接受爬虫或爬行类,您可以通过从爬行类创建爬虫

因此,您可以在开始爬网过程之前创建爬网程序实例,然后检索期望的属性

下面我通过编辑几行原始代码为您提供了一个示例:

import scrapy
from scrapy.crawler import CrawlerProcess


class TestSpider(scrapy.Spider):
    name = 'test'
    start_urls = ['http://httpbin.org/get']

    def parse(self, response):
        self.crawler.stats.inc_value('foo')


process = CrawlerProcess({})
crawler = process.create_crawler(TestSpider)
process.crawl(crawler)
process.start()


stats_obj = crawler.stats
stats_dict = crawler.stats.get_stats()
# perform the actions you want with the stats object or dict
import scrapy
from scrapy.crawler import CrawlerProcess


class TestSpider(scrapy.Spider):
    name = 'test'
    start_urls = ['http://httpbin.org/get']

    def parse(self, response):
        self.crawler.stats.inc_value('foo')


process = CrawlerProcess({})
crawler = process.create_crawler(TestSpider)
process.crawl(crawler)
process.start()


stats_obj = crawler.stats
stats_dict = crawler.stats.get_stats()
# perform the actions you want with the stats object or dict