python - 谷歌地图 API 的命中率限制,但不知道为什么

标签 python geocoding google-maps-api-2

我编写了一个脚本来将地址发送到 Google Maps 的 API 并接收回 Lat 和 Lng。但是,我收到错误消息,指出我在 20 次左右的查询后超出了 Google 的速率限制。有什么我没有考虑的吗?

如果有任何帮助,我将不胜感激。我在使用 API 方面还很陌生,所以更好地理解我达到速率限制的原因会非常有帮助。

从名为 Location 的 csv 文件中读取地址后,下面是我的相关代码。

    for row in locations:
        address = 'XXX, New Haven, CT'
        first = re.search('^(.*),',row[0])
        address = re.sub('XXX',first.group(), address)
        lat, lng = gmaps.address_to_latlng(address)

下面是我的错误信息。

    Traceback (most recent call last):
    File "<input>", line 5, in <module>
    File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/googlemaps-1.
    0.2-py2.7.egg/googlemaps.py", line 310, in address_to_latlng
        return tuple(self.geocode(address)['Placemark'][0]['Point']['coordinates'][1
    ::-1])
      File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/googlemaps-1.
    0.2-py2.7.egg/googlemaps.py", line 262, in geocode
        raise GoogleMapsError(status_code, url, response)
    GoogleMapsError: Error 620: G_GEO_TOO_MANY_QUERIES

最佳答案

每次 gmaps.address_to_latlng 调用都会向 Google 服务器发送一个请求,您只能发出有限数量的请求。

Google 的文档 usage limits :

Use of the Google Geocoding API is subject to a query limit of 2,500 geolocation requests per day. [...] Additionally, we enforce a request rate limit to prevent abuse of the service.

还有 docs on G_GEO_TOO_MANY_QUERIES :

The given key has gone over the requests limit in the 24 hour period or has submitted too many requests in too short a period of time. If you're sending multiple requests in parallel or in a tight loop, use a timer or pause in your code to make sure you don't send the requests too quickly.

所以,按照他们告诉你的去做:

import time

# And then in the loop, pause:
time.sleep(1)

将“1”调整为适当的秒数,这样您就不会用完允许的请求。

关于python - 谷歌地图 API 的命中率限制,但不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7643296/

相关文章:

javascript - 如何将HTML字符串发送到Django模板JavaScript?

python - PyTorch:为什么要创建同一类型层的多个实例?

python SOAP 添加 header

sql - 使用 BigQuery SQL 进行反向地理编码 : How to determine the city closest to a (lat, lon)?

javascript - 如何在Javascript中返回用户的纬度和经度?

android - WebView 链接打不开

android - 如何禁用android map 标记点击自动中心

android - 谷歌地图 v2 实时路线?

python - 在 Python 2.5 中用微秒解析日期时间字符串

MySQL查询拉取一定范围内的经度