我正在尝试用python编写一个简单的网络抓取器,它使用googlemaps
api来查找本地加油站,但由于某种原因,我无法让它自行执行。 当我单步执行它或使用 python 提示符时,代码可以工作,但是当我尝试自行运行代码时,我得到一个INVALID_REQUEST
异常。
这是我的代码:
你需要一个 api 密钥来运行它,但你可以在这里创建一个: https://developers.google.com/places/web-service/get-api-key 运行应用程序需要花钱,但谷歌每月给你 200 美元的信用额度,所以你不需要担心成本。也就是说,我不会把我的贴在这里让大家拿走。
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)
那里有很多停顿,我这样做是因为我认为我请求页面的速度太快了,但它们似乎没有区别。 我还尝试将gmaps
声明移动到函数调用中,但这也没有区别。
这是我收到的回溯错误:
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
我今天刚开始看这个 api,所以我对此很陌生,并且一直在努力在 python 客户端上找到任何真正的文档,所以任何帮助将不胜感激。
从文档中:
在发布
next_page_token
和生效之间有短暂的延迟。在下一页可用之前请求下一页将返回INVALID_REQUEST
响应。使用相同的next_page_token
重试请求将返回下一页结果。
换句话说,在请求下一页之前,您需要稍等片刻。您可以在用户输入时加载下一个结果,或者只是延迟进一步的请求。