|
| 1 | +import json |
| 2 | +import requests |
| 3 | + |
| 4 | +from requests_oauthlib import OAuth1 |
| 5 | + |
| 6 | + |
| 7 | +consumer_key= 'NZTJrWhij8kemtAXmfyhyA' |
| 8 | +consumer_secret = 'JtcAesDkKuNltcKdwR7NEaUkgm8' |
| 9 | +token = 'TCipoxU_lYo55-F3rS10XdnN6f-3-KQI' |
| 10 | +token_secret = '5gAZ99Arn2x_LIOVM25AWy8H84c' |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +url ='https://api.yelp.com/v2/search?term=food&location=San+Francisco' |
| 15 | +r = requests.get(url, auth=auth) |
| 16 | + |
| 17 | +def do_search(term='Food', location='San Francisco'): |
| 18 | + base_url = 'https://api.yelp.com/v2/search' |
| 19 | + term = term.replace(' ', '+') |
| 20 | + location = location.replace(' ', '+') |
| 21 | + url = "{base_url}?term={term}&location={location}".format(base_url=base_url, |
| 22 | + term=term, |
| 23 | + location=location) |
| 24 | + auth = OAuth1(consumer_key, |
| 25 | + consumer_secret, |
| 26 | + token, |
| 27 | + token_secret) |
| 28 | + r = requests.get(url, auth=auth) |
| 29 | + return r.json(), r.text |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +json_data, text_data = do_search() |
| 34 | +python_data = json.loads(text_data) |
| 35 | +print(json.dumps(json_data, indent=4, sort_keys=True)) |
| 36 | + |
| 37 | + |
| 38 | +for i in json_data['businesses']: |
| 39 | + print(i["name"]) |
| 40 | + print(i["phone"]) |
| 41 | + print(i["location"]["display_address"]) |
| 42 | + print(i["location"]["city"]) |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
0 commit comments