Web Scrape Google Places using Google Place API

Bharath Bhaskar
Nov 24, 2018 · 3 min read

Hello There, In this post, we are going to discuss how to use Google Places API to extract nearby places for your work-related projects.

To start with you have to access https://cloud.google.com/ and create an account for yourself and then access console and click on start a project and navigate to credentials and generate an API Token Key. This API Key is used in the request URL.

The Nearby places are searched through values called types (eg- if you want to search for atms the type should be atm) different types can be found in onhttps://developers.google.com/places/supported_types

A Look at Google Places JSON Response

{
"html_attributions" : [],
"next_page_token" : "CqQCHAEAAI-xbApZZIwBOwj6UrcRC99XyIaax5rX3fxzMtMh9tGbtVk0InV7lwS0TjsQd6xah-2P0zuLYthSOgJF36oIiE86RPnykxC6zB7vsVdps6WjtEyr1aZqI9p67f6Pwg0YNsUeplX2o-joATcFLukekDA6ezcqttO9c9qf_K107Jo7WSFxBQTDLKhz6a6BYkVaPfEpQ3oXf-0VSaOYx4M-a5g4hEuhe4msTv-OLbUWfR_xywpu1uceagR_4OEeB9aN8-i4XVdEyCyVcwpEp_ZdWxI0rChdi02JxYRyvxRF5W9h4u1di3CN86EbKyi7TiFIMaga3N_I6gaAVm_UWj4tM9Y9XB1jCcpUPE83jBZIRmb810I3_7Uicy5iXYlDf2j4RRIQyI9Q7h5cwxmCVIzEf5C3JBoUDdc3iS9fAM48dcQUeZ7mUsuKPkE",
"results" : [
{
"geometry" : {
"location" : {
"lat" : 12.9213898,
"lng" : 77.48339279999999
},
"viewport" : {
"northeast" : {
"lat" : 12.9227387802915,
"lng" : 77.4847417802915
},
"southwest" : {
"lat" : 12.9200408197085,
"lng" : 77.48204381970848
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/bank_dollar-71.png",
"id" : "cfafea7c0cab199a62e76a9ce6196dbb7cf10c31",
"name" : "Syndicate Bank Kengeri Satellite Town Branch",
"opening_hours" : {
"open_now" : true
},
"photos" : [
{
"height" : 3264,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/118252296607606392393/photos\"\u003eDamodar nani\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAAgkjbMjeryGvez5EOX9I1cuzY_9fsEQ3TykCFWCoTy5p6l_n0Ouly0oWoQYVbJgpVNkTVh3F078IisYmKQ2yXxkw_WRgmalUXKyaO0EAn9zYg2xnQNFEGZrTVFxoStuZkEhBDlBy48XoiNdftj8NV0zajGhQJqqz2jXH2LtH3sPBTSdpihUep8Q",
"width" : 2448
}
],
"place_id" : "ChIJjwL9k9k-rjsRot9vKgzUpW4",
"rating" : 3,
"reference" : "ChIJjwL9k9k-rjsRot9vKgzUpW4",
"scope" : "GOOGLE",
"types" : [ "atm", "bank", "finance", "point_of_interest", "establishment" ],
"vicinity" : "India"
},

Let’s get to some coding where we search an atm for a locality with said radius(which is in meters) and save the results to a CSV file

import json
import csv
import requests
import time
url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=Latitude,Longitude&radius=1500&type=atm&key=YOUR GOOGLE API KEY'headers={'content-type':'application/json',
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:63.0) Gecko/20100101 Firefox/63.0'}
def csv_write():
csvfile =open('google_maps.csv','w')
writer = csv.writer(csvfile, delimiter=',')
writer.writerow(["name","lat","long"])
response = requests.get(url=url, headers=headers).json()
for obj in response['results']:
writer.writerow ([obj['name'],obj['geometry']['location']['lat'],obj['geometry']['location']['lng']])
print ('next_page_token' in response)
while 'next_page_token' in response:
URL = url + '&pagetoken=' + response['next_page_token']
time.sleep(5)
response = requests.get(url=url, headers=headers).json()
for obj in response['results']:
writer.writerow ([obj['name'],obj['geometry']['location']['lat'],obj['geometry']['location']['lng']])
else:
print(response)
if __name__ == '__main__':
csv_write()

At a time Google response is limited to 20 results per page and at the same time if the result is stored in the 2nd page it gives you a next_page_token key which has to be passed in the URL. Since the response is limited per page we pass a time.sleep call to delay the response by a 5 Seconds this is to ensure that the response doesn’t flag you an error saying Your API limit is exceeded the reason is that the API takes time to load the next page but in this span of time, the query which is passed hits the API .

At last CSV file is saved in the path defined. You can also pass different values of types through a list within the function.

Through this JSON Parsing Method, you can retrieve the places that you need for your Projects. Happy Scrapping!

Bharath Bhaskar

Written by

Data Scientist, R | Python | Web Scraping | SQL

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade