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 reviews 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 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 and 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

Number of Reviews

Currently, the Google Places API returns only 5 last reviews.

Being able to retrieve all the reviews for a given business is only supported if you are a verified business owner and can be done via the Google My Business API: https://developers.google.com/my-business/

Number & Type of Requests

Using Google Places API for free, you can only send up to 100,000 requests per month, and retrieve “Basic Data”.

The Basic Data SKU is triggered when any of these fields are requested: address_component, adr_address, formatted_address, geometry, icon, name, permanently_closed, photo, place_id, plus_code, type, url, utc_offset, vicinity.

On the other hand, there are request types that are paid from the first request. You can check rate limits and pricing details here: https://developers.google.com/places/web-service/usage-and-billing

Completed Code

 

 

Rating: 4.8/5. From 22 votes.
Please wait...

17 Replies to “Google Places API: Extracting Location Data & Reviews”

  1. Thanks. I have 300 addresses in a dataframe. How we can handle them?

    Rating: 1.0/5. From 3 votes.
    Please wait...
    1. Hi Abbas! What do you want to do with these addresses? Generally speaking, you can check this question (read all the answers) to know how to iterate a dataframe.

      Rating: 1.0/5. From 2 votes.
      Please wait...
  2. Thanks a lot for sharing the knowledge. I have used your query but getting an error “SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1056)”. Any help would really be appreciated. Thanks!!

    Rating: 1.0/5. From 1 vote.
    Please wait...
    1. @soumi Try adding the verify=False to this line, as follows:
      requests.get(endpoint_url, params = params, verify=False)

      Rating: 1.0/5. From 1 vote.
      Please wait...
  3. I have a list of addresses that I would like to know the business name that is at each location.

    Rating: 5.0/5. From 1 vote.
    Please wait...
    1. @Jack What about “iterating” the list of addresses using a for loop?

      Rating: 5.0/5. From 1 vote.
      Please wait...
  4. hello
    i have got empty places 🙁 i only change GooglePlaces(“AIzaSyCy-HpjsoOoQAbwbhGdhfE8wCwrtpvqj14”)

    Rating: 5.0/5. From 1 vote.
    Please wait...
    1. ===================PLACE===================
      (‘Name:’, ”)
      (‘Website:’, ”)
      (‘Address:’, ”)
      (‘Phone Number’, ”)
      ==================REWIEVS==================
      ===================PLACE===================
      (‘Name:’, ”)
      (‘Website:’, ”)
      (‘Address:’, ”)
      (‘Phone Number’, ”)
      ==================REWIEVS==================

      No votes yet.
      Please wait...
      1. @leena Are you sure the code written/copied correctly and that you are using the latest versions of all the libraries?

        Rating: 5.0/5. From 1 vote.
        Please wait...
  5. hi,
    how to store it in excel file ?

    Rating: 5.0/5. From 1 vote.
    Please wait...
    1. @leena You can use a Python library like Pandas or CSV to store data into a CSV file.

      No votes yet.
      Please wait...
  6. Traceback (most recent call last):
    File “C:/Users/varsayemlak/AppData/Local/Programs/Python/Python37-32/asdad.py”, line 1, in
    import requests
    ModuleNotFoundError: No module named ‘requests’

    No votes yet.
    Please wait...
    1. @evisofis You must install the Python library “requests” beforehand.

      No votes yet.
      Please wait...
  7. I really need to get in contact with Majid. I need some help and am not asking for you to do it for free. Please contact me.

    No votes yet.
    Please wait...
    1. @Chase – Regarding the malicious Telegram group you found, you can simply “report” it to Telegram on their website, or send them an email at “abuse@telegram.org” and they can take the proper action. This has nothing to do with us or what we do here. All the best! -admin

      No votes yet.
      Please wait...
  8. thank you it is work now, but how to receive more than 5 reviews?

    No votes yet.
    Please wait...
  9. and is it possible to specify the language for the 5 reviews?

    Rating: 5.0/5. From 1 vote.
    Please wait...

Leave a Reply