Yelp Fusion API Python example
Introduction
Have you heard of Yelp before ? Probably yes, it is a crowd sourced review local search service. Today, we are going to demonstrate Yelp’s open API using Python. We are going to implement a simple Python script to search for a business, get its rating, address and phone number. Our script utilizes Yelp Fusion search API to query for business using a search term and location. Sounds good ?
Let us get started…
Yelp developer
Follow the steps below…
- In order to be able to use Yelp Fusion API, we need to register as a developer, create an app and get an API key. This is straight forward, just go to here and the rest is self explanatory.
- If you do not have the requests library installed, do so by running the following command…
1 |
pip install requests |
Implementation
For explanation, take a look at the comments…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# This is needed to perform REST API calls import requests # Developer API key API_KEY= "Enter your api key here" # What you are searching for DEFAULT_TERM = 'Smog Check' # Business location DEFAULT_LOCATION = 'San Jose, CA' # Maximum number of results to return SEARCH_LIMIT = 5 # Busines search end point url = 'https://api.yelp.com/v3/businesses/search' # Heahder should contain the API key headers = {'Authorization': 'Bearer {}'.format(API_KEY)} # Search parameters url_params = { 'term': DEFAULT_TERM, 'location': DEFAULT_LOCATION, 'limit': SEARCH_LIMIT } # Call the API response = requests.request('GET', url, headers=headers, params=url_params) # To get a better understanding of the structure of # the returned JSON object refer to the documentation # For each business, print name, rating, location and phone for business in response.json()["businesses"]: print("{:30} {:5} {:20} {:10}".format( business["name"], business["rating"], business["location"]["display_address"][0], business["display_phone"])) |
Example output
We can go fancy and implement command line arguments but this is only a demonstration. Run the script from the command line…
1 |
python yelp.py |
If everything is ok, we should get an output that looks like the following…
1 2 3 4 5 |
MT Smog Check Star Station 5.0 210 San Jose Ave (408) 297-2051 Green Star Smog Check 4.0 88 Keyes St (408) 423-0000 Smog Hut Star Station 5.0 1394 N 4th St (408) 451-7000 Smoguys Star Station 5.0 5835 Winfield Blvd (408) 227-1575 Smog Man Test and Repair 5.0 646 Auzerais Ave (408) 999-0699 |
Thanks for visiting. If you questions, please use the comments section below…
Hi Mohammed – How can I use this script in my website to show a list of local businesses.