-1

I am trying to get more than 20 restaurants in googlemaps.places_nearby(). so, I am using the page_token mentioned in a question here on stackoverflow.

here's the code:

import time
with open('apikey.txt') as f:
    api_key = f.readline()
    f.close
import googlemaps 
gmaps = googlemaps.Client(api_key)
locations = [(30.0416162646183, 31.187637297709912),(30.038828662868447, 31.21133524457125),(29.848956883337507, 31.334386571579085),(30.047845819479956, 31.262317130706496),(30.05312112490655, 31.24665544474578),(30.044482967408886, 31.23572953125353),(30.02023034028819, 31.176992671570066),(30.055592085960892, 31.18411299557052),(30.0512387364253, 31.20328697618034),(30.027741592767295, 31.174344307489818),(30.043337503059586, 31.17587613443309),(30.049286828183856, 31.181250916540794),(30.043423144171197, 31.187248209629644),(30.040934096091647, 31.183299998037857),(30.038296379882215, 31.189823130232988),(29.960107152991863, 31.250999388927262) , (29.83911392727571, 31.30468827173587) , (29.842752004034566, 31.332961535887694)]
search_string = "كشري"
distance = 10000
kosharies = []
for location in locations:
    response = gmaps.places_nearby(location = location , keyword = search_string , name = 'كشري' , radius = distance)
    kosharies.extend(response.get('results'))
    next_page_token = response.get('next_page_token')
    
    while next_page_token:
        time.sleep(2)
        another_response = gmaps.places_nearby(location = location , keyword = search_string , name = 'كشري' , radius = distance ,\
                                              page_token = next_page_token)
        kosharies.extend(another_response.get('results'))
        next_page_token = another_response.get('next_page_token')
        

I provided 18 different locations. Judging by the fact that each request must give back exactly 20 or less. I looked manually in the 18 locations, I know that each location has more than 20 restaurants in this category!

I tried going for a while loop over the page_tokens, but no luck. the shape of the dataset returned is (142 , 18) rows, columns.

I'd appreciate your help so much.

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
  • 1
    Did you check the API docs? – roganjosh Feb 08 '23 at 14:42
  • Yes, also I saw a lot of questions on this issue and even tried chat GPT. I just can't get it to work – Ismail Awad Feb 08 '23 at 14:43
  • 4
    ChatGPT is useless if you want definitive answers. This would be defined in the API docs – roganjosh Feb 08 '23 at 14:44
  • I SWEAR TO ALL GODS THAT EVER EXISTED IN THE WHOLE WORLD I LOOKED AT THE DOCS – Ismail Awad Feb 08 '23 at 14:48
  • 1
    Does this answer your question? [Paging on Google Places API returns status INVALID\_REQUEST](https://stackoverflow.com/questions/21265756/paging-on-google-places-api-returns-status-invalid-request) – roganjosh Feb 08 '23 at 14:49
  • OK, I see you have tried that method. I retract my dupe sorry – roganjosh Feb 08 '23 at 15:01
  • 2
    It is stated in the [docs](https://developers.google.com/maps/documentation/places/web-service/search-nearby#PlaceSearchPaging): _By default, each response returns up to 20 establishment results per query; however, each search can return as many as 60 results, split across three pages_. What's the issue ? – Maurice Meyer Feb 08 '23 at 15:05
  • 3
    1) What do the subsequent requests return? 2) `name` is deprecated, you only need `keyword` and 3) It should be `pagetoken` and not `page_token`. Back to first comment: *Did you check the API docs?* – MrUpsidown Feb 08 '23 at 16:47

1 Answers1

0

This seems to work just fine, please check your requests' parameters carefully:

First request:

location=30.0416162646183,31.187637297709912&radius=10000&keyword=كشري

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=30.0416162646183,31.187637297709912&radius=10000&keyword=%D9%83%D8%B4%D8%B1%D9%8A

Returns 20 results and a next_page_token. Second request uses only that token, with pagetoken (not page_token):

https://maps.googleapis.com/maps/api/place/nearbysearch/json?pagetoken=AfLeUgPos3GU6Ew0BWV52EyHv9ay7Q7H7N-44c0pSTfb0JN039qifhKotQiUPlF9O3P1jdeSJarnR72GHMmUW1jkBS0ErfYe_jpi9cUCx--XO1n1DUp_eF0XZ_Ue4KJ7_l5h6FjaE_1f2Z0G9q3lwGLg-0Ch5n4p7KaTKMq8CET8QX-lXa2_ssemCiFGXrOj6vn4wDXKNRqYAOGquNiaq9_3RWUs_k7Epe_uCicW94hC-PX90nisZxuW-zy3SBAmuRJpL4pV3CA9CWH0ygBigHWy88Sle6b1S-4k2GWK72n-eAMEEmmAOziqt1ETCy-li92pqjP4BgDv7jKYCD2uKgL3jRWdGyglroNkP02HFX49qHNbNrl0MfhKn_lTvw6zSjwF001nDOnYq5mgE8KCTe3b7cDxGgZVYWFFKwyNuswXiUPTy9D4lXhRJX7oRF6DH7YF-lH7faLpeh2eZBMP73AtFJlPX7B0c4_riCgeCK2C0Pvz_lBivx4VtkOehmuYfOVwBpi54rJW4-iZnrIjW0NrRD7HibL76MWyr_njLIf5eLx9Tl2PYiwTOj3Vd6Rjafry7b15M69Jhku1C22AVhy0R4HfYd5LHFn38N_ILL8PhDaMk3S2TKkzrohYyomrbvlffrBKUDij9Bbggvoy2s3iHrg6N-Em3SNrTPcWS65chZUALp_kve04rfU4wjhKow

This also returns 20 results and a final next_page_token. Third request:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?pagetoken=AfLeUgPos3GU6Ew0BWV52EyHv9ay7Q7H7N-44c0pSTfb0JN039qifhKotQiUPlF9O3P1jdeSJarnR72GHMmUW1jkBS0ErfYe_jpi9cUCx--XO1n1DUp_eF0XZ_Ue4KJ7_l5h6FjaE_1f2Z0G9q3lwGLg-0Ch5n4p7KaTKMq8CET8QX-lXa2_ssemCiFGXrOj6vn4wDXKNRqYAOGquNiaq9_3RWUs_k7Epe_uCicW94hC-PX90nisZxuW-zy3SBAmuRJpL4pV3CA9CWH0ygBigHWy88Sle6b1S-4k2GWK72n-eAMEEmmAOziqt1ETCy-li92pqjP4BgDv7jKYCD2uKgL3jRWdGyglroNkP02HFX49qHNbNrl0MfhKn_lTvw6zSjwF001nDOnYq5mgE8KCTe3b7cDxGgZVYWFFKwyNuswXiUPTy9D4lXhRJX7oRF6DH7YF-lH7faLpeh2eZBMP73AtFJlPX7B0c4_riCgeCK2C0Pvz_lBivx4VtkOehmuYfOVwBpi54rJW4-iZnrIjW0NrRD7HibL76MWyr_njLIf5eLx9Tl2PYiwTOj3Vd6Rjafry7b15M69Jhku1C22AVhy0R4HfYd5LHFn38N_ILL8PhDaMk3S2TKkzrohYyomrbvlffrBKUDij9Bbggvoy2s3iHrg6N-Em3SNrTPcWS65chZUALp_kve04rfU4wjhKow

This also returns 20 results, but no next_page_token.

Bear in mind that there is no guarantee on the amount of results for any given request. The API can return up to 60 results (in 3 pages), but it can also return any number of them from 0 to 60. This can happen more easily with Place Nearby Search because, as the name indicates, it is meant to return only results that are near by; even though it will accept a large radius value, it usually doesn't return results that are far away.

miguev
  • 4,481
  • 21
  • 41