Google Places API: Extracting Location Data & Reviews

Google places API allows developers to access a wealth of information from Google’s database for over 100 million places including location data, contact information, user ratings and reviews and more.

In this tutorial, you will learn how to create a reusable class to read and extract location related information from Google Places API. This tutorial will help you if you want to extract business’s name, address, phone number, website, and reviews.

How to Get an API key

1- Login to your Google Cloud Console

2- From top navigation bar click “Select a project”

3- In the new window click “New project”

 

4- Type a name for your project and click on “Create” 

 

5- From left side navigation go to “APIs & Services > Library”

 

6-  From Maps section select “Places API” or search for “Places API” in the search box.

 

7- Click on “Enable”

8- Go to Credentials tab

 

9- Click on “Create Credentials”

 

10- Click on “API Key”

 

11- Copy your generated API Key and store it somewhere.

 

Congratulations! You’ve got your Google Places API key successfully. Now let’s get started with coding.

 

Dummy Class Object

Let’s create a class which does nothing for now. You will  pass your API key in the class constructor and set the  apiKey  attribute so you can access it later easily. We will complete our class step by step.

Search for Places

To get place details, you need to search for places and get the place IDs first. Fortunately there is an API endpoint for this.

With this endpoint you will send a GPS Coordinate and a radius to the API and it will return the nearby places by your defined radius. Also there is a filter called  types which can filter out only the types of the places that you are interested in. Like school  or restaurant .

Note: Here is a list of all valid types:

 

Lets add this search function to our class.

Note: Google places API can return the results in JSON or XML format. We will be using JSON format in this tutorial.

As you can see we are sending 4 parameters to the api and get back our json result. Then load it in a Python dictionary using  json.loads  function.

But still there is something missing in our function. Each search can return maximum 60 results and there will be 20 results per page. So you will need to paginate through the results if there is more that 20 results in your search.

If there is more pages the api will return  next_page_token  with the results and you have to submit this value to the api with the same search parameters to get the rest of the results.

Note: There is a delay until the  next_page_token is issued and validated. So you need to put a small sleep time like 2 seconds between each request. Otherwise you will get an  INVALID_REQUEST status.

So let’s add pagination to our function. At the beginning we are creating an empty list for the found places and extending it with results from the search API.

Now our function with return a list containing the search results (Max 60 places) but still we don’t have all the details like user reviews.

 

Place Details

To get the complete details we have to use another API endpoint. So let’s create another function to get the place details.

Again we have to submit some parameters to the place details API to get the results. Some parameters are required some while others are optional.

 

Required parameters:

key : your API key.

placeid : An identifier that uniquely identifies a place, returned from the Place Search.

 

Optional parameters:

language : The language code, indicating in which language the results should be returned. See the list of supported languages and their codes (default is EN).

fields : One or more fields, specifying the types of place data to return, separated by a comma.

There is 3 categories for the fields parameter.

Basic:  address_component, adr_address, alt_id, formatted_address, geometry, icon, id, name, permanently_closed, photo, place_id, plus_code, scope, type, url, utc_offset, vicinity 

Contact:  formatted_phone_number, international_phone_number, opening_hours, website 

Atmosphere:  price_level, rating, review 

So let’s create our function to get the place details.

It’s going to be very similar to our first function. We just need to use different URL and parameters.

This is how this function looks like.

Note: This function expects the fields parameter to be a list of strings (Valid fields string from above) then in convert the list to a comma separated string using  ",".join(fields)

 

So here is our complete class.

Now let’s use this class to retrieve some real information.

 

Example: Getting User Reviews for a Place Using GooglePlaces Class

In this example, you will see how to retrieve some information about places including  place name,  user review and ratings, address, phone number and website.

First you need to search for places as stated before. So go to maps.google.com and search for the area you are interested in. then click anywhere on the map and a small box will show up in the bottom of the page.

Copy the GPS coordinates. We will use it to search the area.

We have everything we need now. Let’s write some code.

Initialize  GooglePlaces class with your API key.

search the and store the the results in a list.

Note: this will return nearby restaurant up to 100 meters away and maximum of 60 places.

Now  places variable contains our search results. Every place has a  place_id . we will use the place identifier to retrieve more details about it.

let’s define the fields which we want to retrieve.

fields = ['name', 'formatted_address', 'international_phone_number', 'website', 'rating', 'review']

And finally retrieve the details.

You have all the details in  details dictionary now. Here I’m just going to print the details but you can store it in csv or excel file or even a database.

If you run this code in your terminal, it will print something like the image below. I have added some separators so it’ easier to read.

As you can see in the image all the info we requested is printed on the the screen so you can process them easily.

Note:  some variable ( website,name, addrees, phone_number ) are wrapped inside a try/except block just in case if the API  doesn’t return those info.

 

Limitations

Currently the Google Places API returns only 5 last reviews. In order to have access to more than 5 reviews with the Google Places API you have to purchase Premium data Access from Google. That premium plan will grant you access to all sorts of additional data points you have to shell out a pretty penny.

If you are a Business owner wanting to retrieve all of your reviews, you can do so but first you have to get verified and could do this through the MyBusiness API more info here: https://developers.google.com/my-business/

Also you can check rate limits and pricing details here: https://developers.google.com/places/web-service/usage-and-billing

Completed Code

 

 

I speak Python!

Majid Alizadeh is a freelance developer specialized in web development, web scraping and automation. He provides high quality and sophisticated software for his clients. Beside Python he works with other languages like Ruby, PHP and JS as well.

Rating: 5.0/5. From 2 votes.
Please wait...

Leave a Reply