更新由 scrapyd 控制的爬虫代码

Update spider code controlled by scrapyd

install/activate 由 scrapyd 控制的蜘蛛的正确方法是什么?

我使用 scrapyd-deploy 安装了一个新的 spider 版本;目前有一份工作 运行。我是否必须使用 cancel.json 停止作业,然后安排新作业?

回答我自己的问题:

我写了一个小 python 脚本来停止所有 运行ning 蜘蛛。在 运行 执行此脚本后,我 运行 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