Python 如何在scrapy spider中迭代参数列表?

Python 如何在scrapy spider中迭代参数列表?,python,web-scraping,scrapy,web-crawler,restapi,Python,Web Scraping,Scrapy,Web Crawler,Restapi,您好,我正在尝试在scrapy spider命令中传递参数列表。我可以运行1个参数。但无法为参数列表执行此操作。请帮忙。这是我试过的。 我可以用这个命令运行它 scrapy crawl airbnbweather -o BOSTON.json -a geocode="42.361","-71.057" 很好用。但是我怎样才能遍历地理代码列表呢 list = [("42.361","-71.057"),(&quo

您好,我正在尝试在scrapy spider命令中传递参数列表。我可以运行1个参数。但无法为参数列表执行此操作。请帮忙。这是我试过的。

我可以用这个命令运行它

scrapy crawl airbnbweather -o BOSTON.json -a geocode="42.361","-71.057"

很好用。但是我怎样才能遍历地理代码列表呢

list = [("42.361","-71.057"),("29.384","-94.903"),("30.384", "-84.903")]

您只能将字符串用作spider参数(),因此应该将列表作为字符串传递,并在代码中进行解析。 以下几点似乎起到了作用:

import scrapy
import json
import ast


class AirbnbweatherSpider(scrapy.Spider):
    name = 'airbnbweather'
    allowed_domains = ['www.wunderground.com']

    def __init__(self, geocode, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.geocodes = ast.literal_eval(geocode)

    def start_requests(self):
        for geocode in self.geocodes:
            yield scrapy.Request(
                url="https://api.weather.com/v3/wx/forecast/daily/10day?apiKey=6532d6454b8aa370768e63d6ba5a832e&geocode={0}{1}{2}&units=e&language=en-US&format=json".format(geocode[0],"%2C",geocode[1]))

然后可以按如下方式运行爬虫程序:

scrapy crawl airbnbweather -o BOSTON.json -a geocodes='[("42.361","-71.057"),("29.384","-94.903"),("30.384", "-84.903")]'
scrapy crawl airbnbweather -o BOSTON.json -a geocodes='[("42.361","-71.057"),("29.384","-94.903"),("30.384", "-84.903")]'