我想把从spider收集到的所有统计信息存储到一个以json格式存储的输出文件中。但是,我得到了这个错误:
'MemoryStatsCollector'对象没有属性'get_all'
:文档中提到stats.get_all
是获取所有存储的方法。正确的实现方法是什么?
import scrapy
from scrapy import signals
from scrapy import crawler
import jsonlines
class TestSpider(scrapy.Spider):
name = 'stats'
start_urls = ['http://quotes.toscrape.com']
def __init__(self, stats):
self.stats = stats
@classmethod
def from_crawler(cls, crawler, *args,**kwargs):
#spider = super(TestSpider, cls).from_crawler(crawler, *args,**kwargs)
stat = cls(crawler.stats)
crawler.signals.connect(stat.spider_closed, signals.spider_closed)
return stat
def spider_closed(self):
#self.stats = stat
txt_file = 'some_text.jl'
with jsonlines.open(txt_file, 'w') as f:
f.write(self.stats.get_all())
def start_requests(self):
for url in self.start_urls:
yield scrapy.Request(
url=url,
callback=self.parse
)
def parse(self, response):
content = response.xpath('//div[@class = "row"]')
for items in content:
yield {
'some_items_links':items.xpath(".//a//@href").get()
}
1条答案
按热度按时间fkaflof61#
结果发现该方法没有
get_all
,而我不得不输入get_stats()
,文档提供了一些示例:stats的文档中提供了一些进一步的信息。
工作部分:
输出量: