【问题标题】:Passing json arguments to a spider in scrapy将json参数传递给scrapy中的蜘蛛
【发布时间】:2014-07-05 17:58:28
【问题描述】:

我应该将一些从 json 文件中获取的参数传递给蜘蛛。我已经读过可以通过scrapyd使用 schedule.json 但我不明白如何传递 json 文件。你们有人有经验吗?

【问题讨论】:

    标签: json scrapy parameter-passing scrapyd


    【解决方案1】:

    您不使用 JSON 文件传递​​参数。 Scrapyd 有一个 JSON API,您可以在其中传递参数。 (例如$ curl http://localhost:6800/schedule.json -d project=myproject -d spider=somespider -d myargument="value"

    你可以处理通过kwargs传递的参数:

    class MySpider(Spider):
    
        name = 'somespider'
    
        def __init__(self, *args, **kwargs):
            super(MySpider, self).__init__(*args, **kwargs)
            self.myargument = kwargs.get('myargument', '')
    

    请参阅http://scrapyd.readthedocs.org/en/latest/api.html 了解更多信息。

    【讨论】:

    • 我已经看到了,但我不明白将 json 文件作为蜘蛛的参数传递到哪里
    • 您不能传递文件本身。您可以做的最接近的事情是传递文件的路径。 (例如-d /path/to/file)并让您的蜘蛛在其代码中的某个位置处理它。
    • 如果我在 AWS EC2 linux 实例上托管我的 scrapyd 实例并且我需要传递一个 json 文件,我将如何处理? stackoverflow.com/questions/42284726/…
    【解决方案2】:

    我有同样的问题(我想传递一个json文件给蜘蛛来实现一个简单的分布式爬虫系统。

    我只是通过将json文件转换为字符串作为scrapyd中的参数来解决它。

    【讨论】:

      猜你喜欢
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多