python - 我怎样才能获得scrapy的抓取速度?

标签 python web-scraping scrapy

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

我可以看到日志级别 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}

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

最佳答案

这是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)

这样就可以得到requests/min的平均速度

关于python - 我怎样才能获得scrapy的抓取速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34081887/

相关文章:

c# - 有没有办法实现 ZeroMQ 全双工 channel ?

r - 使用 rvest 进行网络抓取时如何修复 R 'Error in UseMethod("xml_find_all")' 中的以下错误?

python - 如何在不同的输入网站上同时多次运行 Scrapy 抓取工具并写入不同的输出文件?

cmd - 调度 scrapy 命令 scrapy crawl

python - 通过 Scrapy 中的元从连续的并行解析函数中产生值

python - 链接到事件点击时如何从网站抓取数据?

python - 将文件作为参数传递给 Docker 容器

python - 将 argparse.subparser 与 bool 值一起使用

python - 如何获取GAE的行id?

python - 使用 BeautifulSoup 抓取 Tripadvisor 评论时出现 ValueError