python - Scrapy集群分布式爬虫策略

标签 python redis scrapy apache-kafka apache-zookeeper

Scrapy 集群很棒。它可用于使用 Redis 和 Kafka 执行巨大的连续抓取。它确实很耐用,但我仍在努力找出满足我特定需求的最佳逻辑的更精细细节。

在使用 Scrapy Clusters 时,我能够设置三级蜘蛛,它们依次从彼此接收 url,如下所示:

site_url_crawler >>> gallery_url_crawler >>> content_crawler

(site_crawler 会向 gallery_url_crawler 提供类似 cars.com/gallery/page:1 的内容。gallery_url_crawler 可能会向 content_crawler 提供 12 个 url,这些 url 可能看起来像 cars.com/car:1234、cars.com/car:1235、cars .com/car:1236 等。content_crawler 将收集我们想要的所有重要数据。)

我可以通过添加到 gallery_url_crawler.py

来做到这一点
    req = scrapy.Request(url)
    for key in response.meta.keys():

        req.meta[key] = response.meta[key]
        req.meta['spiderid']= 'content_crawler1'
        req.meta['crawlid'] = 'site1'

    yield req   

通过这种策略,我可以将 url 从一个爬虫提供给另一个爬虫,而无需等待后续爬虫完成。然后创建一个队列。为了充分利用集群,我希望在有瓶颈的地方添加更多的爬虫。在此工作流程中,瓶颈在最后,即抓取内容时。所以我尝试了这个:

site_url_crawler >>> gallery_url_crawler >>> content_crawler + content_crawler + content_crawler

由于缺乏更好的说明,我只是想展示我使用了最后一个蜘蛛的三个实例来处理更长的队列。

但似乎 content_crawler 的每个实例都耐心等待当前 content_crawler 完成。因此,生产力没有提高。

我最后的想法是这样的:

site_url_crawler >>> gallery_url_crawler >>> content_crawler1 + content_crawler2 + content_crawler3

所以我尝试使用单独的蜘蛛来接收最终队列。

不幸的是,我无法对此进行试验,因为我无法像在 gallery_url_crawler.py 中那样将 kafka 消息传递给 demo.inbound:

    req = scrapy.Request(url)
    for key in response.meta.keys():

        req.meta[key] = response.meta[key]
        req.meta['spiderid']= 'content_crawler1'
        req.meta['spiderid']= 'content_crawler2'
        req.meta['crawlid'] = 'site1'

    yield req   

(注意额外的 spiderid)上面没有工作,因为我认为它不能将一条消息分配给两个不同的蜘蛛...... 和

    req1 = scrapy.Request(url)
    req2 = scrapy.Request(url)
    for key in response.meta.keys():

        req1.meta[key] = response.meta[key]
        req1.meta['spiderid']= 'content_crawler1'           
        req1.meta['crawlid'] = 'site1'

    for key2 in response.meta.keys():
        req2.meta[key2] = response.meta[key2]
        req2.meta['spiderid']= 'content_crawler2'
        req2.meta['crawlid'] = 'site1'
    yield req1
    yield req2

我认为没有用,因为 dupefilter 踢出了第二个,因为它认为它是一个骗子。

无论如何,我只希望最终以一种允许我随时启动多个蜘蛛实例、从队列中拉取并重复的方式使用集群。

最佳答案

事实证明,分发 url 是基于 IP 地址的。一旦我在不同的机器上建立集群,即。每个蜘蛛的不同机器 url 流动并且都从队列中获取。

http://scrapy-cluster.readthedocs.org/en/latest/topics/crawler/controlling.html

Scrapy Cluster comes with two major strategies for controlling how fast your pool of spiders hit different domains. This is determined by spider type and/or IP Address, but both act upon the different Domain Queues.

关于python - Scrapy集群分布式爬虫策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35980855/

相关文章:

python - 如何修复语法错误 : invalid syntax on a Raspberry Pi?

python - 将正则表达式解析为 Word

Perl redis hset 没有返回正确的 retcode?

python - 通过startproject创建Scrapy项目时出错

python - 无法在我的代码中找到错误所在?

python - 使用 django,你可以做一些类似于 user = User.new(params[ :user]) during a form post?

python - Python if 语句中何时需要 `pass`?

python - scrapy-cluster : socket.错误:[Errno 98]地址已在使用中

Redis CPU 峰值

java - 如何使用 Redis 适配器写入 Geode,然后使用 Geode 客户端读取/响应事件?