安装/激活由scrapyd控制的蜘蛛的正确方法是什么?
我使用scrapyd-deploy安装了一个新的蜘蛛版本;一份工作目前正在运行。我是否必须使用cancel.json
停止工作,然后安排新工作?
答案 0 :(得分:0)
回答我自己的问题:
我写了一个小的python脚本来阻止所有正在运行的蜘蛛。运行此脚本后,我运行scrapyd-deploy
,然后重新启动我的蜘蛛。
我仍然不确定这是否是scrapy专业人员会这样做的方式,但对我来说这看起来很合理。
这是脚本(替换PROJECT
的值以适合您),它需要requests
包(pip install requests
):
import requests
import sys
import time
PROJECT = 'crawler' # replace with your project's name
resp = requests.get("http://localhost:6800/listjobs.json?project=%s" % PROJECT)
list_json = resp.json()
failed = False
count = len(list_json["running"])
if count == 0:
print "No running spiders found."
sys.exit(0)
for sp in list_json["running"]:
# cancel this spider
r = requests.post("http://localhost:6800/cancel.json", data={"project":PROJECT, "job": sp["id"]})
print "Sent cancel request for %s %s" % (sp["spider"], sp["id"])
print "Status: %s" % r.json()
if r.json()["status"] != "ok":
print "ERROR: Failed to stop spider %s" % sp["spider"]
failed = True
if failed:
sys.exit(1)
# poll running spiders and wait until all spiders are down
while count:
time.sleep(2)
resp = requests.get("http://localhost:6800/listjobs.json?project=%s" % PROJECT)
count = len(resp.json()["running"])
print "%d spiders still running" % count