【问题标题】:How can I get the scraping speed in scrapy?如何在scrapy中获得抓取速度?
【发布时间】:2015-12-05 01:27:36
【问题描述】:

我正在使用代理池抓取网站。它们中很少有非常快,但很少有代理非常慢,这让我想知道是否有任何方法可以检测中间件中的网页抓取速度(scrapedPages/minute),以便我可以丢弃较慢的代理。

我可以看到日志级别INFO 每分钟在屏幕上打印这个速度。

2015-12-04 11:28:50 [scrapy] INFO: Crawled 10 pages (at 10 pages/min), scraped 10 items (at 10 items/min)

但我无法在中间件中获得这种速度。这是我迄今为止尝试过的。

class getSpeedstats(object):
    def __init__(self, stats):
        self.stats = stats

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

     def process_request(self, request, spider):
        print self.stats.get_stats()

上面的代码给了我这个输出:

{'log_count/DEBUG': 784, 'scheduler/dequeued': 408, 'log_count/INFO': 10, 'downloader/response_count': 392, 'downloader/response_status_count/200': 392, 'response_received_count': 392, 'scheduler/enqueued/memory': 408, 'downloader/response_bytes': 3209679, 'start_time': datetime.datetime(2015, 12, 4, 3, 48, 41, 31403), 'scheduler/dequeued/memory': 408, 'scheduler/enqueued': 408, 'downloader/request_bytes': 101321, 'downloader/request_method_count/GET': 407, 'downloader/request_count': 407, 'item_scraped_count': 391}

{'log_count/DEBUG': 786, 'scheduler/dequeued': 409, 'log_count/INFO': 11, 'downloader/response_count': 393, 'downloader/response_status_count/200': 393, 'response_received_count': 393, 'scheduler/enqueued/memory': 409, 'downloader/response_bytes': 3217865, 'start_time': datetime.datetime(2015, 12, 4, 3, 48, 41, 31403), 'scheduler/dequeued/memory': 409, 'scheduler/enqueued': 409, 'downloader/request_bytes': 101575, 'downloader/request_method_count/GET': 408, 'downloader/request_count': 408, 'item_scraped_count': 392}

但我仍然无法破译如何计算蜘蛛的速度。谁能告诉我是否有其他方法可以做到这一点?

【问题讨论】:

    标签: python web-scraping scrapy


    【解决方案1】:

    这是LogStats扩展中计算出来的量,基本上是每分钟current_pages - previous_pages,可以查看here

    现在这不是 IMO 的实际费率,因为它只考虑特定的分钟,而总平均速度会更好,因为只需使用:

    pages = self.stats.get_value('response_received_count')
    print pages/((datetime.now() - self.stats.get_value('start_time')).seconds/60.0)
    

    这样就可以得到请求/分钟的平均速度

    【讨论】:

    • 谢谢。这消除了一些疑虑。这个datetime(2015, 12, 4, 9, 27, 45, 718073) 代码意味着什么?是当前日期时间前 1 分钟吗?还是蜘蛛的开始时间?
    • 对不起,我是stats.start_time
    • 将零作为输出,直到响应数超过分钟。
    • 感谢@eLRuLL 的回答。该代码确实给出了平均爬网速度,但是对于在进行广泛爬网(其中 ip 每隔一段时间发生变化)时何时考虑代理更慢的决策时,很难分析哪个代理更慢。您提供的链接确实解决了我计算速度的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 2017-04-06
    • 2014-02-20
    • 1970-01-01
    相关资源
    最近更新 更多