美文网首页
scrapy 数据收集stats collection

scrapy 数据收集stats collection

作者: zenos876 | 来源:发表于2019-08-14 10:01 被阅读0次

Scrapy提供了一种方便的工具,用于以键/值的形式收集统计数据,其中值通常是计数器。 该工具称为统计收集器.
统计收集器为每个打开的spider保留一个统计表,当spider打开时它会自动打开,当spider关闭时它会关​​闭。

现为了统计404页面, 并收集404页面的数量和url

简单的例子

在scrapy的spider middleware httperror中有个参数handle_httpstatus_list, 这个列表主要用来判断哪些相应状态的response需要处理

  1. 先在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 = []
  1. 在parse函数中定义使用统计stats对象的方法
    def parse(self, response):
        if response.status == 404:
            self.failed_urls.append(response.url)
            self.crawler.stats.inc_value('failed_url')
  1. debug调试


其它通常见方法

常见方法

官方文档

相关文章

网友评论

      本文标题:scrapy 数据收集stats collection

      本文链接:https://www.haomeiwen.com/subject/aluwjctx.html