【发布时间】: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