4

I send a request via google maps library and I get a result of 20 places.

My code looks like that

gmaps = googlemaps.Client(key='MY_KEY_IS_HERE')

x = gmaps.places(query=['restaurants', 'bakery', 'cafe', 'food'],location='40.758896, -73.985130',radius=10000)

print len(x['results']) # outputs 20 (which is maximum per 1 page result)

and afterwards I want to request a 2nd page and I use it I suppose correctly.

print gmaps.places(page_token=x['next_page_token'].encode())

and it returns a weird error

    File "/Users/././testz.py", line 15, in <module>
    print gmaps.places(page_token=x['next_page_token'].encode())
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/googlemaps/client.py", line 356, in wrapper
    result = func(*args, **kwargs)
TypeError: places() takes at least 2 arguments (2 given)

I use .encode() because the 'next_page_token' outputs in unicode and googlemaps requires 'str'.

Any idea what am I doing wrong and how can I fix it ?

nexla
  • 434
  • 7
  • 20

1 Answers1

3

It should work if you pass all parameters again (in addition to the page_token). Note that you need to add a couple of seconds delay to allow the token to be validated on Google's servers.

import googlemaps
import time

k="your key here"

gmaps = googlemaps.Client(key=k)

params = {
    'query': ['restaurants', 'bakery', 'cafe', 'food'],
    'location': (40.758896, -73.985130),
    'radius': 10000
}

x = gmaps.places(**params)
print len(x['results']) # outputs 20 (which is maximum per 1 page result)
print x['results'][0]['name']

params['page_token'] = x['next_page_token']

time.sleep(2)
x1 = gmaps.places(**params)

print len(x1['results'])
print x1['results'][0]['name']
user2314737
  • 27,088
  • 20
  • 102
  • 114
  • oh that does indeed work! Thanks a lot ! I was confused because documentation of the API says _'Setting pagetoken will cause any other parameters to be ignored. The query will execute the same search as before, but will return a new set of results'_ – nexla Nov 19 '17 at 13:09
  • @nexla Yeah, the documentation for page_token is not clear. – user2314737 Nov 19 '17 at 19:05
  • Can I loop it to get 100 restaurants? unfortunately, I getting the next page token error. – Daman deep Oct 12 '21 at 07:02