Google Maps API displays 20 results on one page. By using page_token
parameter, it is possible to get more than 20 results. The API fetches maximum 60 results for a query. The query can be refined should we need more result - run the query on sub-division level instead of country.
The following code loads the first 20 results, if the JSON file has "next_page_token"
key then it recursively loads the subsequent pages.
The scripts available in my GitRepo
And here's are the saved results in Gist. Nepalese Restaurants in Europe. Interestingly, there are almost 80 Nepalese restaurants in Finland.
The script uses 3 libraries. time
is needed to delay the API call. Google has a limitation on request per second.
import json
import requests
import time
The next is declare the varible and load first JSON
# Declare API Key & Query
key = 'AIzaSyXXX-XXXX'
query = 'Nepalese+Restaurants+Finland'
url = 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=' + str(query) + '&key=' + str(key)
# Parse JSON
jsonfile = requests.get(url)
f = jsonfile.json()
The below function recursively populates the result.
# Recursive Function
def getjson(url, pagetoken, key, query):
time.sleep(3) # API has requests per second limitation
if pagetoken == '':
print('url', url)
pass
else:
print('e1url', url)
# Reset URL
url = 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=' + str(query) + '&key=' + str(key)
url = url + '&pagetoken=' + pagetoken
jsonfile = requests.get(url)
f = jsonfile.json()
# append array with desired data from JSON
for count, name in enumerate(f['results']):
names.append((f['results'][count]['name']))
address.append(f['results'][count]['formatted_address'])
# print(f) #DEBUG
# Assign NEW Page Token
try:
pagetoken = f['next_page_token']
except KeyError:
pagetoken = ''
pass
# Recursive call
getjson(url, pagetoken, key, query)