I am following along this yelp API tutorial: http://letstalkdata.com/2014/02/how-to-use-the-yelp-api-in-python/
And I have the code below. I am basically trying to separate out the elements of the dictionary of results in a meaningful way. Right now I just get a whole stream of data... how do I loop through the dictionary in such a way that it presents all the keys and values cleanly one after the other?
import rauth
import time
def main():
locations = [(39.98,-82.98),(42.24,-83.61),(41.33,-89.13)]
api_calls = []
for lat,long in locations:
params = get_search_parameters(lat,long)
api_calls.append(get_results(params))
#Be a good internet citizen and rate-limit yourself
time.sleep(1.0)
##Do other processing
for key in api_calls:
print key
def get_results(params):
#Obtain these from Yelp's manage access page
consumer_key = "CONSUMER_KEY_HERE"
consumer_secret = "CONSUMER_SECRET_HERE"
token = "TOKEN_HERE"
token_secret = "TOKEN_SECRET_HERE"
session = rauth.OAuth1Session(
consumer_key = consumer_key
,consumer_secret = consumer_secret
,access_token = token
,access_token_secret = token_secret)
request = session.get("http://api.yelp.com/v2/search",params=params)
#Transforms the JSON API response into a Python dictionary
data = request.json()
session.close()
return data
def get_search_parameters(lat,long):
#See the Yelp API for more details
params = {}
params["term"] = "restaurant"
params["ll"] = "{},{}".format(str(lat),str(long))
params["radius_filter"] = "2000"
params["limit"] = "10"
return params
if __name__=="__main__":
main()
api_calls
is a list of dicts. Each of the dicts has a key businesses
, which references a list of dicts, one for each business at the location.
You want to iterate over the list of API call results. For each entry, iterate over the list of businesses and print the values for the keys name
and phone
for location in api_calls:
for business in location['businesses']:
print business['name'], business['phone']
Output:
Creole Kitchen 6143723333
Los Potosinos 6148876895
Royal Fish, Shrimp, & Chicken 6144296002
Shades Restaurant 6142916555
Zanzibar Brews 6147580111
Grass Skirt Tiki Room 6144293650
That Food Truck 6149463797
Los Potosinos 6148876895
Asian Wok II 6142527661
Cafe On Long 6142524552
Beezy's 7344859625
Sidetrack 7344831035
The Bomber Restaurant 7344820550
Maìz Mexican Cantina 7343406010
Bona Sera Cafe 7343406335
Red Rock Downtown Barbecue 7343402381
The Wurst Bar 7344856720
ABC Microbrewery 7344802739
Cafe Ollie 7344828050
Aubree's Pizzeria & Grill 7344838888
Brandys 8152201744
Mark Allen's American Kitchen 8152200642
Bayou Express 8157808651
Demilio's Italian Deli 8152244808
Fourth Street Bakery & Cafe 8152241927
Peru Pizza House Restaurant 8152237408
Right Spice Supper Club 8152239824
Maples Supper Club the 8152231938
Liberty Family Restaurant 8152243240
Pizzas by Marchelloni 8152201564