Google Places API:OVER_QUERY_LIMIT 问题是间歇性的

我知道以前有人问过这个问题,但我的问题与其他线程略有不同。

我编写了一个 python 脚本来读取随机地址并返回它们的位置 ID。这是我的 excel 文件的样子:

http://img.mukewang.com/62c4063800019a1104190232.jpg

这是我的代码:


from googleplaces import GooglePlaces

import pandas as pd

import time

import xlrd

YOUR_API_KEY = <my_key>


google_places = GooglePlaces(YOUR_API_KEY)

loc = r"C:\Users\user\Desktop\test.xlsx"


wb = xlrd.open_workbook(loc)

sheet = wb.sheet_by_index(0)


for i in range(1,sheet.nrows):

    read_address = sheet.cell_value(i,0)

    query_result = google_places.text_search(

        query=''+read_address)

    for place in query_result.places:

        print(place.name)

        print(place.place_id)

控制台输出:


C:\Users\csjeemah\PycharmProjects\googleplaces\venv\Scripts\python.exe C:/Users/csjeemah/PycharmProjects/googleplaces/googleplaceID.py

777 Brockton Ave

ChIJmf-3TW6b5IkR1rx0eohvqPQ

30 Memorial Dr

ChIJXa_KpmGD5IkRYtpt2IsXmqg

Traceback (most recent call last):

  File "C:/Users/csjeemah/PycharmProjects/googleplaces/googleplaceID.py", line 27, in <module>

    query_result = google_places.text_search(

  File "C:\Users\csjeemah\PycharmProjects\googleplaces\venv\lib\site-packages\googleplaces\__init__.py", line 353, in text_search

    _validate_response(url, places_response)

  File "C:\Users\csjeemah\PycharmProjects\googleplaces\venv\lib\site-packages\googleplaces\__init__.py", line 175, in _validate_response

    raise GooglePlacesError(error_detail)

googleplaces.GooglePlacesError: Request to URL https://maps.googleapis.com/maps/api/place/textsearch/json?query=250+Hartford+Avenue%2C+Bellingham+MA+2019&radius=3200&language=en&key=mykey&sensor=false failed with response code: OVER_QUERY_LIMIT

在上面的控制台屏幕截图中,它返回前 2 个地址的结果,然后发生错误。现在我明白我收到此错误的原因是由于我可以发出的请求数量受到限制,但你可以从上图看:


只有 9 个地址。

http://img1.mukewang.com/62c4064e000180f513160112.jpg

如果我缩短一些地址,如下所示:

http://img3.mukewang.com/62c4065e0001494d04210212.jpg

它现在在失败之前检索更多地址:


C:\Users\csjeemah\PycharmProjects\googleplaces\venv\Scripts\python.exe C:/Users/csjeemah/PycharmProjects/googleplaces/googleplaceID.py

777 Brockton Ave

ChIJmf-3TW6b5IkR1rx0eohvqPQ

30 Memorial Dr

ChIJt_pwFGsUc2sR-ve6pnT9DlA

30 Memorial Dr

ChIJJatf08pZIWsRr2u-ppt2CyI

我还尝试在每次查询后添加睡眠几秒钟,但失败了,谁能告诉我问题是什么?


繁星淼淼
浏览 160回答 2
2回答

扬帆大鱼

图片中的错误 json 清楚地表明您已经超出了 places API 的每日请求配额。如果您想继续,您需要将您的 GCP 项目与结算帐号相关联

米脂

直接来自Google Maps Platform Web Services 的文档使用限制您可以通过以下方式超出 Google Maps Platform 网络服务使用限制:每天发送太多请求。发送请求太快,即每秒请求太多。发送请求太快太久或以其他方式滥用 Web 服务。超出其他使用限制,例如 Elevation API 中每个请求的点数。收到状态码为 OVER_QUERY_LIMIT 的响应后,您的应用程序应确定已超出哪个使用限制。这可以通过暂停 2 秒并重新发送相同的请求来完成。如果状态码仍为 OVER_QUERY_LIMIT,则您的应用程序每天发送的请求过多。否则,您的应用程序每秒发送的请求过多。url = "MAPS_API_WEBSERVICE_URL"attempts = 0success = Falsewhile success != True and attempts < 3:&nbsp; raw_result = urllib.urlopen(url).read()&nbsp; attempts += 1&nbsp; # The GetStatus function parses the answer and returns the status code&nbsp; # This function is out of the scope of this example (you can use a SDK).&nbsp; status = GetStatus(raw_result)&nbsp; if status == "OVER_QUERY_LIMIT":&nbsp; &nbsp; time.sleep(2)&nbsp; &nbsp; # retry&nbsp; &nbsp; continue&nbsp; success = Trueif attempts == 3:&nbsp; # send an alert as this means that the daily limit has been reached&nbsp; print "Daily limit has been reached"
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python