scrapyd schedule.json setting 傳入多個值

apuyuseng發表於2019-08-07

使用案例:

import requests

adder='http://127.0.0.1:6800'
data = {
        'project':'v1',
        'version':'12379',
        'setting':['ROBOTSTXT_OBEY=True','CONCURRENT_REQUESTS=32']
}

resp = requests.post(adder,data=data)

  

 

 

問題解決思路:

版本1.2文件中:

    • setting (string, optional) - a Scrapy setting to use when running the spider

setting 是字串,那麼多個字串該如何呢。

 

通過原始碼 site-packages/scrapyd/webservice.py

class Schedule(WsResource):

    def render_POST(self, txrequest):
        args = native_stringify_dict(copy(txrequest.args), keys_only=False)
        settings = args.pop('setting', [])
        settings = dict(x.split('=', 1) for x in settings)
        args = dict((k, v[0]) for k, v in args.items())
        project = args.pop('project')
        spider = args.pop('spider')
        version = args.get('_version', '')
        spiders = get_spider_list(project, version=version)
        if not spider in spiders:
            return {"status": "error", "message": "spider '%s' not found" % spider}
        args['settings'] = settings
        jobid = args.pop('jobid', uuid.uuid1().hex)
        args['_job'] = jobid
        self.root.scheduler.schedule(project, spider, **args)
        return {"node_name": self.root.nodename, "status": "ok", "jobid": jobid}

 可以發現 setting 在這裡是list物件,且裡面元素是字元,字串必須包含=符號。

以此我們嘗試傳入setting:['DEBUG=True','HOST_NAME=axdda']

結果真的可以。對於比較複雜的設定,下次有需求再研究吧。

相關文章