user9860931
user9860931

Reputation:

Python, Google Places API

I'am trying to get all places of type restaurant in certain location within a given radius using python request to Google Places API. Here's my simple code:

import requests
import json

APIKEY = "AIzaSyBFx8hqftDOlrSWRTiOSowjwfeS1OQtBpw"

def findPlaces(loc=("51.1079","17.0385"),radius=3000, pagetoken = None):
   lat, lng = loc
   type = "restaurant"
   url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={lat},{lng}&radius={radius}&type={type}&key={APIKEY}{pagetoken}".format(lat = lat, lng = lng, radius = radius, type = type,APIKEY = APIKEY, pagetoken = "&pagetoken="+pagetoken if pagetoken else "")
   print(url)
   response = requests.get(url)
   res = json.loads(response.text)
   print(res)
   for result in res["results"]:
      info = ";".join(map(str,[result["name"],result["geometry"]["location"]["lat"],result["geometry"]["location"]["lng"],result.get("rating",0),result["place_id"]]))
      print(info)
   pagetoken = res.get("next_page_token",None)
   return pagetoken


pagetoken = None
while True:
     pagetoken = findPlaces(pagetoken=pagetoken)
     if not pagetoken:
         break

However, even thought it's working good for the first page of the results, it's failing on getting to second page with no obvious reason (it simply returns INVALID REQUEST from the API). Moreover, what's interesting, since I print my url to the console, I can follow this very specific url simply by clicking it, and when I do, it's working perfectly, API returns desired list of restaurants.

I thought it was an issue with encoding strings for urls and I replaced format with python's urlencode, however - result stays the same. Since I run out of ideas, I am asking: had anyone something to do with such an issue?

Thanks in advance for any help.

Upvotes: 3

Views: 10226

Answers (1)

Mehdi Kazemi
Mehdi Kazemi

Reputation: 525

I tested it with new latitude and longitude and got 3 pages each 20 which sums up to 60 as the google docs says.

https://developers.google.com/places/web-service/search

next_page_token contains a token that can be used to return up to 20 additional results. A next_page_token will not be returned if there are no additional results to display. The maximum number of results that can be returned is 60. There is a short delay between when a next_page_token is issued, and when it will become valid.

import requests
import json

APIKEY = "AIzaSyBFx8hqftDOlrSWRTiOSowjwfeS1OQtBpw"

def findPlaces(loc=("35.701474","51.405288"),radius=4000, pagetoken = None):
   lat, lng = loc
   type = "restaurant"
   url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={lat},{lng}&radius={radius}&type={type}&key={APIKEY}{pagetoken}".format(lat = lat, lng = lng, radius = radius, type = type,APIKEY = APIKEY, pagetoken = "&pagetoken="+pagetoken if pagetoken else "")
   print(url)
   response = requests.get(url)
   res = json.loads(response.text)
   # print(res)
   print("here results ---->>> ", len(res["results"]))

   for result in res["results"]:
      info = ";".join(map(str,[result["name"],result["geometry"]["location"]["lat"],result["geometry"]["location"]["lng"],result.get("rating",0),result["place_id"]]))
      print(info)
   pagetoken = res.get("next_page_token",None)

   print("here -->> ", pagetoken)

   return pagetoken

# pagetoken = "CpQFhwIAADQWOcVI1wll-B869Z24El48rXw18gKoab_keD65V18zFEvPjKIfrS79Pc_vXJcZQtOuF0RObQG20ph-GE3ssP3k1fu8zsYbw5g3UPbSjAvQLdXkdD1qAWztXj7hc5Kxc4pYRyGM1_ljVOHg3Py_zSlYscnoNjCvRua2MDQgusCsEquNqGREFdvhjDkbeMhEFYxHucTnIn96OxIJEpamePTHsBooYyPBaa_ejGZ_C99QeDjpSkSKBgEe3aL1uWKlYhsGKh7biQUR5rKsKPodwccLIrW8Gr5tag3NH0sLPExHHvqzlpkj--KIuydTVjPH7u2zHxmPByServ2S5xjXYUBRr-ly3e1xPsVMhZZH9TxfttCIHLscBvpvCswIfaGYdl3bEzsrFISfpp0rpKtlp9gWGY7Tbk2n6s3etCHQEHn2qmM8bsJwkZV81pUWN0j9C9RX-ywOyIKY2yp1w_Iq1mRwOwY4mckbicOoooHiV6JER4xe7Kizw9hbXOnezn_NMk15TLwRoXlfL1s73uwogo-VWE8c-V1HqRpWQSyudRhLwhOEclrICXIdxICOgTgYO1z57xCEerw3QUL_7MPDrlbbh_AlX8I6Jfe8IhQ1Fkqu_njatm6aBTjkp2CSqlvZJpI_Lrv330VcyFEqBkGn7NJew3I9xofSrBaXFa8ABi6DXQm6-yC32OEyf7GHNXINjT1IB0yh6KR6c0qzaqiqOzKcuuai9XqEMQNNKyi6EuhzH5TP9YA56N3JhnXRFhs2aWHZhLlieVI6_uqzpZSgYjUem8aQrMTlmHw0kIYU8I-Ca041C4Zm2gMezwygRrhzsOoAmbmu96nft0KuIWTB3A_xGVKYQ2qjb2KRM7nsglnSEhDoNs8EhvuIm0FQs30YSCp5GhRO3b3Tn5rsLuwiWgu8hwEGhL0S1A"
pagetoken = None

while True:
     pagetoken = findPlaces(pagetoken=pagetoken)
     import time
     time.sleep(5)

     if not pagetoken:
         break

Upvotes: 9

Related Questions