【问题标题】:What is the best manner to run many Scrapy with multiprocessing?使用多处理运行许多 Scrapy 的最佳方式是什么?
【发布时间】:2015-08-14 09:55:17
【问题描述】:

目前我将 Scrapy 与多处理一起使用。我做了一个 POC,以便运行许多蜘蛛。 我的代码是这样的:

#!/usr/bin/python 
# -*- coding: utf-8 -*-
from multiprocessing import Lock, Process, Queue, current_process

def worker(work_queue, done_queue):
    try:
        for url in iter(work_queue.get, 'STOP'):
            status_code = run_spider(action)
    except Exception, e:
        done_queue.put("%s failed on %s with: %s" % (current_process().name, action, e.message))
    return True


def run_spider(action):
    os.system(action)

def main():
    sites = (
        scrapy crawl level1 -a url='https://www.example.com/test.html',
        scrapy crawl level1 -a url='https://www.example.com/test1.html',
        scrapy crawl level1 -a url='https://www.example.com/test2.html',
        scrapy crawl level1 -a url='https://www.example.com/test3.html',
        scrapy crawl level1 -a url='https://www.anotherexample.com/test4.html',
        scrapy crawl level1 -a url='https://www.anotherexample.com/test5.html',
        scrapy crawl level1 -a url='https://www.anotherexample.com/test6.html',
        scrapy crawl level1 -a url='https://www.anotherexample.com/test7.html',
        scrapy crawl level1 -a url='https://www.anotherexample.com/test8.html',
        scrapy crawl level1 -a url='https://www.anotherexample.com/test9.html',
        scrapy crawl level1 -a url='https://www.anotherexample.com/test10.html',
        scrapy crawl level1 -a url='https://www.anotherexample.com/test11.html',
    )

    workers = 2
    work_queue = Queue()
    done_queue = Queue()
    processes = []

    for action in sites:
        work_queue.put(action)

    for w in xrange(workers):
        p = Process(target=worker, args=(work_queue, done_queue))
        p.start()
        processes.append(p)
        work_queue.put('STOP')

    for p in processes:
        p.join()

    done_queue.put('STOP')

    for status in iter(done_queue.get, 'STOP'):
        print status

if __name__ == '__main__':
    main()

您认为,运行多个 Scrapy 实例的最佳解决方案是什么?

最好为每个 URL 启动一个 Scrapy 实例,或者启动一个带有 x 个 URL 的蜘蛛(例如:1 个带有 100 个链接的蜘蛛)?

【问题讨论】:

    标签: python python-2.7 web-scraping scrapy multiprocessing


    【解决方案1】:

    最好为每个 URL 启动一个 Scrapy 实例或启动一个带有 x 个 URL 的蜘蛛(例如:1 个带有 100 个链接的蜘蛛)?

    启动一个 Scrapy 实例绝对是一个糟糕的选择,因为对于每个 URL,您都会遭受 Scrapy 本身的开销。

    我认为最好在蜘蛛之间平均分配 URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 2016-05-26
      • 1970-01-01
      • 2017-11-21
      • 2020-07-18
      • 1970-01-01
      • 2011-02-26
      相关资源
      最近更新 更多