user6727195 user6727195 - 1 month ago 6
JSON Question

Inserting 2 lists into a URL

I have 2 lists like this:

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


And I would like to add them to a URL to create a search to use an API. Here is what I have:

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)


The URL is going to an API then saving the data as a JSON file. I am inputting the Type list into the url where 'type=%s'
I would like to input the list of zipcodes that correspond to the word in the Type list where . This code works for iterating through Type, but I have to manually change the zipcode in the URL to the corresponding word. Is it possible to put list items in two different spots?

Answer

Try this code,

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

I think, it'll help you out.

Comments