4

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 Here is the documentation page: https://googlemaps.github.io/google-maps-services-python/docs/2.4.4/#module-googlemaps.exceptions

In the .places def, I need to enter page_token in string format in order to get next 10 listings. When I run the codes below, it kept showing me INVALID_REQUEST

Here is my code:

places_result = gmaps.places(query="hotels in Singapore", page_token='')
for result in places_result['results']:
    print(result['name'])
try :
places_result = gmaps.places(query="hotels in Singapore", page_token=places_result['next_page_token'])
except ApiError as e:
    print(e)
else:
    for result in places_result['results']:
    print(result['name'])
Johnny Koh
  • 171
  • 7
  • Related question: [Paging on Google Places API returns status INVALID\_REQUEST](https://stackoverflow.com/questions/21265756/paging-on-google-places-api-returns-status-invalid-request) – user2314737 Nov 19 '17 at 12:53

1 Answers1

12

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')

def printHotels(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')
Johnny Koh
  • 171
  • 7
  • 1
    In the ´google places api`docummentation it states that the server needs a x time to register the `next_page_token`. This is why you need the `sleep` – Bruno Francisco Nov 04 '17 at 13:59
  • @Johnny Koh: I almost approved [your suggested edit to my answer](https://superuser.com/review/suggested-edits/783774) on [SU] and let you have the 2 reputation points, despite the lamentable lack of research behind it.  But you violated the three most important rules of editing (specifically, editing other people’s contributions):  (1) ***Never** delete* something that somebody else wrote unless you’re ***110%*** sure that it’s 100% wrong.  If you have alternative information, *add* it to what’s already there.  (2) Do your homework and get your facts right.  … (Cont’d) – G-Man Says 'Reinstate Monica' Aug 25 '18 at 02:50
  • (Cont’d) …  (It’s true that you can get to the “Folder Options” window via the “Organize” menu, but it’s not the only way.)  (3) Proofread everything.  Then do it again.  Spell correctly; use correct grammar, punctuation, etc.  (You misspelled “Organize” in [your edit](https://superuser.com/review/suggested-edits/783774).) – G-Man Says 'Reinstate Monica' Aug 25 '18 at 02:50