Scrapy提供了一种方便的工具,用于以键/值的形式收集统计数据,其中值通常是计数器。 该工具称为统计收集器.
统计收集器为每个打开的spider保留一个统计表,当spider打开时它会自动打开,当spider关闭时它会关闭。
现为了统计404页面, 并收集404页面的数量和url
简单的例子
在scrapy的spider middleware httperror中有个参数handle_httpstatus_list, 这个列表主要用来判断哪些相应状态的response需要处理
- 先在spider中定义handle_httpstatus_list,并初始化failed_urls
class CnblogsSpider(scrapy.Spider):
name = 'cnblogs'
allowed_domains = ['news.cnblogs.com']
start_urls = ['http://news.cnblogs.com/abc'] #404页面
curr_page = 1
handle_httpstatus_list = [404]
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.failed_urls = []
- 在parse函数中定义使用统计stats对象的方法
def parse(self, response):
if response.status == 404:
self.failed_urls.append(response.url)
self.crawler.stats.inc_value('failed_url')
-
debug调试
其它通常见方法

网友评论