Getting Started with the Yelp API | by Raymond Willey | Towards Data Science

This post is part one of a series in which we explore how small businesses can leverage Yelp data to better understand their position in local markets. Before diving into the nitty-gritty, I would like to start by telling you a bit about the inspiration and objectives of this project. Of course, if you are just interested in the technical part, you can skip to it here.
I moved to New York City in 2012, and in the last seven years, I have already seen some of my favorite local businesses go under. Slowly but surely, the big-name brands start moving-in. Sure, we could talk all day about economies of scale and rising rents, but the power of the purse also allows large corporations to take advantage of massive quantities of data in ways that small firms can’t. It’s time we start closing that gap.
Through this series, we are going to perform an analysis of one of my favorite Mexican restaurants in Astoria: Chano’s Cantina.
Unlike other, more prominent neighborhoods of New York City, Astoria maintains a unique identity that is freckled with an array of local cuisines. No doubt, it benefits from being a neighborhood of the most diverse borough in the city. But this also means that every restaurant has a wide array of competitors, making it that much more difficult for any local restaurant to differentiate itself. Indeed, a quick search of Yelp for Mexican restaurants in Astoria yields 24 pages of results.
Our goal is simple: to identify the top five to ten competitors to the business of interest, then use natural language processing and sentiment analysis to determine what the firm does well and what it needs to work on. To do this will require a host of data science techniques, which we will go through one at a time through each part of this series. These include, but are not limited to:
Accessing data using the Yelp API
K-Means Clustering
Web Scraping
Sentiment Analysis/NLP
Supervised Machine Learning
Neural Networks
Now, you may be asking yourself, how is a small business that is not well-versed in this type of analysis supposed to implement all of this? Ultimately we want to deploy this in a clean module that requires little more than a few basic inputs:
Company Name (as it appears on Yelp): Chano’s Cantina
Location (Zip or City, St): Astoria, NY
Search Terms (any phrase or combination of phrases that a potential customer might use to find the business on Yelp): Mexican restaurant bar
Without further adieu, let’s start building and get set up with the Yelp API.
First things first: if you want to access data using the Yelp API, Yelp needs to be able to authenticate your identity. This is accomplished by generating an access token through your Yelp account. If you don’t already have a Yelp account, you will need to create one. By clicking on the link above, you should end up on a page that looks like this:
It’s a good idea to create a new token with a different App Name for each, separate project. The mandatory fields are:
App Name
Industry
Contact Email
Description
No need to overthink these, simply fill them out as best you can, agree to the terms and conditions (don’t worry, I checked, and there’s no HumancentiPad clause), and then confirm for the robot that you are not a robot. On the next page, you’ll be provided with a Client ID and Token.
You’ll notice that I have mine blurred out, and that’s because you don’t want to share your token with others. If someone with malintent were to get ahold of your token, they could potentially use it in ways that would get your account suspended, or worse. In other words:
Ideally, you’ll want to save these as environment variables. For more detailed information on this topic, this piece is a great place to start.
For now, you can hardcode these into your Jupyter Notebook as follows:
client_id = your_unique_client_idapi_key = your_unique_api_key
Now that we have that out of the way, we can start making calls to the Yelp API for new data. In our case, we want to search for Mexican restaurants & bars in Astoria, NY, and save the results to a dataframe. The first step is to install the Yelp API via the command line.
pip install yelpapi
With the API installed, you can now load it into any Jupyter Notebook with:
from yelpapi import YelpAPIyelp_api = YelpAPI(api_key)
Easy peasy! Now, a request to the Yelp API is going to require three parts:
Search Term: Search terms used in a standard Yelp search.
Location: City, St or Zip Code
Search Limit: How many results do you want (max 50)?
We want to find Mexican restaurants & bars in Astoria, NY, so we pass the required data into a search query like this:
term = 'Mexican restaurant bar'location = 'Astoria, NY'search_limit = 50response = yelp_api.search_query(term = term,                                 location = location,                                 limit = search_limit)type(response)--------------OUTPUT:dict
As you can see, we save the search results to a response variable, and it is a dictionary.
With further inspection, we would see that this dictionary has three keys:
Businesses
Total
Region
For our purposes, we are interested in businesses data. With further evaluation, you will find that the data in this key is a list of dictionaries, each of which represents a different business, and all of which have the same keys. This allows us to set the list of keys as column names, and use the values as data in a dataframe. We can accomplish this with a few lines of code:
cols = list(response['businesses'][0].keys())data = pd.DataFrame(columns=cols)for biz in response['businesses']:    data = data.append(biz, ignore_index=True)data.head()
And voila! We have a dataframe that contains all the information we would typically see when using the traditional Yelp search, but in a format that we can use for analysis.
As you can see, accessing data using the Yelp API is very simple and quite handy. In the next post, we’ll have a look at the categorical tags, and use K-Means Clustering to find out which restaurants present themselves most similarly to Chano’s Cantina. We will treat these as the most direct competitors for a more in-depth analysis.
For more information on the broader functionality of the Yelp API, check out the documentation.