将2个列表插入URL

时间:2016-11-01 17:42:51

标签: json python-2.7 urllib

我有两个这样的列表:

Type = ['Homeless+Shelter','Food+Pantry','Seniors']
Where = ['55410','55414','54669']

我想将它们添加到URL以创建搜索以使用API​​。这就是我所拥有的:

for elem in Type:
    url = 'https://api.citygridmedia.com/content/places/v2/search/where?type=%s&where=55410&format=json&publisher='PUBLISHER_KEY'&rpp=50' % (elem)
    urllib.urlretreieve(url, 'CityGrid_Search.json)

URL将转到API,然后将数据保存为JSON文件。我正在输入类型列表到网址,其中' type =%s' 我想输入与Type列表中的单词对应的zipcodes列表。此代码适用于迭代Type,但我必须手动将URL中的zipcode更改为相应的单词。是否可以将列表项放在两个不同的位置?

1 个答案:

答案 0 :(得分:0)

试试这段代码,

Type = ['Homeless+Shelter','Food+Pantry','Seniors']
Where = ['55410','55414','54669']

PUBLISHER_KEY = ""

for typeObj, whereObj in zip(Type, Where):
    url = 'https://api.citygridmedia.com/content/places/v2/search/where?type=%s&where=%s&format=json&publisher=%s&rpp=50' % (typeObj, whereObj, PUBLISHER_KEY)
    print url

我想,它会帮助你。