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.