0

I'm trying to write a simple web-scraper in python that uses the googlemaps api to find local gas stations, but for some reason, I can't get it to execute by itself. When I step through it, or use the python prompt, the code works, but when I try to run the code by itself, I get an INVALID_REQUEST exception.

Here's my code:

You need an api key to run this, but you can make one here: https://developers.google.com/places/web-service/get-api-key Running the application costs money, but google gives you a $200 a month credit, so you shouldn't need to worry about the cost. That said, I'm not pasting mine here for everyone to take.

import googlemaps
from googlemaps import places
import time

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


def find_stations() -> list:
  print("Collecting gas station data.")
  stations = []

  print('Collecting result data')
  time.sleep(2)
  search_result = places.places_nearby(gmaps, '42.144735,-93.534612', 160935, keyword='gas station')
  iter = 1
  while True:
    stations += search_result['results']
    if 'next_page_token' not in search_result:
      break
    else:
      iter += 1
      print("Collecting page {}".format(iter), end='\r')
      token = search_result['next_page_token']
      print("The token is: {}".format(token))
      search_result = places.places_nearby(gmaps, '42.144735,-93.534612', 160935, keyword='gas station',
                                           page_token=token)
      time.sleep(1)

  return stations


if __name__ == "__main__":
  for s in find_stations():
    print(s)

There's a lot of pauses in there, I did that because I thought maybe I was requesting the pages too fast, but they don't seem to make a difference. I also tried to move the gmaps declaration into the function call, but this also did not make a difference.

Here is the traceback error that I'm getting:

Collecting gas station data.
Collecting result data
The token is: CrQCIgEAAF6QgiE83iz0sERAFSCJ2pAta_xnIID4DWdDIBcOnp89mZ_UWEkDbSRT5eRmGdj4fQ4kqnQAPzEdvsuzMhhAZzfJMbd6yH97aBvU6V1GRL-fVbS5d4yo-fAEcA-9WABaNneCzSp_JzHMdSa1qv7dKSn1d57ltnw_I9g2V6Lw0DHmGYATanhf9g8tbRT9qDbNNbmC3WSdr5nL0ZuPKB9xmx4Q5AISSYGy4gw_sqSsW7NyMPMCuKpZ0oOhl9bfN1nYnEwD_7SHegt1o7we2OBlYIRqGawcUHvxvabkYtCz9G0flxOckzNqNh3PD1jIBmr4xM1AwBvjxmDxbJudsw9evsXrzIqIoewYInh9sz-DbyGnb_N8f9TXN4xU9ljXve-Zz96YXWWQwh_yM8LGhd5elHMSEBUWS3IRS9S59Rd9deU7ZpQaFIYdprNd8Ysj-xbA9cKPkmhdI80D
Traceback (most recent call last):
  File "/home/aaron/Workspace/projects/gas_webscraper/maps_test.py", line 32, in <module>
    for s in find_stations():
  File "/home/aaron/Workspace/projects/gas_webscraper/maps_test.py", line 25, in find_stations
    page_token=token)
  File "/home/aaron/anaconda3/envs/webscraping/lib/python3.7/site-packages/googlemaps/places.py", line 144, in places_nearby
    rank_by=rank_by, type=type, page_token=page_token)
  File "/home/aaron/anaconda3/envs/webscraping/lib/python3.7/site-packages/googlemaps/places.py", line 235, in _places
    return client._request(url, params)
  File "/home/aaron/anaconda3/envs/webscraping/lib/python3.7/site-packages/googlemaps/client.py", line 253, in _request
    result = self._get_body(response)
  File "/home/aaron/anaconda3/envs/webscraping/lib/python3.7/site-packages/googlemaps/client.py", line 282, in _get_body
    raise googlemaps.exceptions.ApiError(api_status)
googlemaps.exceptions.ApiError: INVALID_REQUEST

I just started looking at this api today, so I'm pretty new to this and have struggled to find any real documentation on the python client, so any help would be appreciated.

10
  • please show the traceback by editing your question to include it Commented Jan 1, 2020 at 21:46
  • sure thing, I just added it Commented Jan 1, 2020 at 21:48
  • it's complaining about the token, is it well formed? Commented Jan 1, 2020 at 21:57
  • How do I know if a token is well formed? I printed it out, but it doesn't seem to be anything out of the ordinary, added it to the question above in the traceback Commented Jan 1, 2020 at 22:00
  • you don't need to add the token to the post but simply making a request outside of your code with that token can eliminate that as being the issue if it is Commented Jan 1, 2020 at 22:04

1 Answer 1

1

From the documentation:

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.

In other words, you need to wait a bit before requesting the next page. You could load the next results on user input, or just delay the further requests.

Sign up to request clarification or add additional context in comments.

2 Comments

using the parameter page_token is correct, as per the github source code
All right. I'll remove that from my answer to avoid confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.