Python Client For Google Maps Services's Places Couldn't Pass Page_token
I'm trying out Python Client for Google Maps Services to pull a list of places using Places API. Here is the GitHub page: https://github.com/googlemaps/google-maps-services-python
Solution 1:
Alright, after hours of trial and error. I noticed I need to add a time.sleep(2) to make it work. I'm not sure why but it works.
It failed with time.sleep(1), time.sleep(2) and above will solve the problem.
Hopefully someone can shed some light to the reason behind.
Here is my code that works to retrieve Places and go to the next page until the end. Remember to replace 1. your key at 'YOUR KEY HERE' and 2. the string you want to search at 'SEARCH SOMETHING'.
import googlemaps
import time
gmaps = googlemaps.Client(key='YOUR KEY HERE')
defprintHotels(searchString, next=''):
try:
places_result = gmaps.places(query=searchString, page_token=next)
except ApiError as e:
print(e)
else:
for result in places_result['results']:
print(result['name'])
time.sleep(2)
try:
places_result['next_page_token']
except KeyError as e:
print('Complete')
else:
printHotels(searchString, next=places_result['next_page_token'])
if __name__ == '__main__':
printHotels('SEARCH SOMETHING')
Post a Comment for "Python Client For Google Maps Services's Places Couldn't Pass Page_token"