Vizag
Vizag

Reputation: 773

Google Places API in Python

I am working with Google Places API in python to look for anything(given as query) in a city/town/place (given as location). Following is a snippet from my code:

from googleplaces import GooglePlaces, types, lang


YOUR_API_KEY = 'xxxx'
google_places = GooglePlaces(YOUR_API_KEY)

query_result = google_places.text_search(
        query='Play Schools', location = 'Mumbai, India')
print(len(query_result.places))
for place in query_result.places:
    print (place.name)
    print (place.place_id)
    print('\n')

if (query_result.has_next_page_token):
query_result_next_page = google_places.text_search(
        pagetoken=query_result.next_page_token)

Now the problem is that my code restricts the number of results to 20 and there are more than 20 results returned by Google Maps on the same query. How do I go about retrieving all the results?

EDIT: I added the last 3 lines of code to try and get the results on the next page but I am thrown with INVALID_REQUEST error.

Thanks for reading my question.

Upvotes: 1

Views: 3157

Answers (2)

Vizag
Vizag

Reputation: 773

I found out what was going wrong. The system was throwing INVALID_REQUEST because the next_page_token wasn't valid (there is a small delay b/w between when a next_page_token is issued, and when it will become valid). I compiled the first half of the code (barring the last three lines) and then compiled the last three lines and it worked!! So to the best of my knowledge, the delay was causing the error.

So now all I have to do is put a timer of 1-2 seconds before the last if condition.

Thanks everyone who commented and responded to this question. You are the heroes of this community.

Upvotes: 2

O_o
O_o

Reputation: 1103

By default, each Nearby Search or Text Search returns up to 20 establishment results per query; however, each search can return as many as 60 results, split across three pages. If your search will return more than 20, then the search response will include an additional value — next_page_token. Pass the value of the next_page_token to the pagetoken parameter of a new search to see the next set of results. If the next_page_token is null, or is not returned, then there are no further results. There is a short delay between when a next_page_token is issued, and when it will become valid. Requesting the next page before it is available will return an INVALID_REQUEST response. Retrying the request with the same next_page_token will return the next page of results.

Check this url: HERE

Add the PagerToken, as an additional parameter with your HTTP request url like this

https://maps.googleapis.com/maps/api/place/nearbysearch/json?pagetoken=CpQCAgEAAFxg8o-eU7_uKn7Yqjana-HQIx1hr5BrT4zBaEko29ANsXtp9mrqN0yrKWhf-y2PUpHRLQb1GT-mtxNcXou8TwkXhi1Jbk-ReY7oulyuvKSQrw1lgJElggGlo0d6indiH1U-tDwquw4tU_UXoQ_sj8OBo8XBUuWjuuFShqmLMP-0W59Vr6CaXdLrF8M3wFR4dUUhSf5UC4QCLaOMVP92lyh0OdtF_m_9Dt7lz-Wniod9zDrHeDsz_by570K3jL1VuDKTl_U1cJ0mzz_zDHGfOUf7VU1kVIs1WnM9SGvnm8YZURLTtMLMWx8-doGUE56Af_VfKjGDYW361OOIj9GmkyCFtaoCmTMIr5kgyeUSnB-IEhDlzujVrV6O9Mt7N4DagR6RGhT3g1viYLS4kO5YindU6dm3GIof1Q&key=YOUR_API_KEY

Copy paste this url on a browser and check it carefully. Also, please read the documentation url carefully. Cheers!

Upvotes: 2

Related Questions