Python 谷歌地理编码API超过查询限制问题
我试图运行一个脚本,将地址(大约1000个)更改为地理坐标,但由于某种原因,在输入列表中的第50个地址中,我得到了一个超过查询限制的响应 为了避免查询限制,我已经在循环中添加了一个time.sleep命令,但出于某种原因,它显示我再次超出了限制 有人能帮忙吗?(仅供参考,我正在笔记本电脑上运行)Python 谷歌地理编码API超过查询限制问题,python,google-maps,google-api,geocoding,google-geocoder,Python,Google Maps,Google Api,Geocoding,Google Geocoder,我试图运行一个脚本,将地址(大约1000个)更改为地理坐标,但由于某种原因,在输入列表中的第50个地址中,我得到了一个超过查询限制的响应 为了避免查询限制,我已经在循环中添加了一个time.sleep命令,但出于某种原因,它显示我再次超出了限制 有人能帮忙吗?(仅供参考,我正在笔记本电脑上运行) 地理编码API有每秒查询数(QPS)限制。您不能发送超过50个QP 此限制记录在 虽然您不再局限于每天的最大请求数(QPD),但地理编码API仍有以下使用限制: 每秒50个请求(QPS),计算为客户
地理编码API有每秒查询数(QPS)限制。您不能发送超过50个QP 此限制记录在 虽然您不再局限于每天的最大请求数(QPD),但地理编码API仍有以下使用限制:
- 每秒50个请求(QPS),计算为客户端和服务器端查询的总和
我希望这有帮助 “运行大约50行后发出警告”是什么意思。你是如何得出这个结论的。你在逐行调试它吗。如果是,那么代码的哪一部分会抛出错误?您总共发送了多少个请求?谷歌也有每日查询限制。@shiva很抱歉这个含糊不清的表达。我在输入列表中的第50个地址收到API的超查询限制响应。找到解决方案了吗?几乎有相同的问题。我使用python库(答案中的链接)修改了我的代码,它成功了@lacroixant我不确定你能在这里找到我的代码有什么不同。所以基本上,我的请求必须从“谷歌地图”库中完成。我的代码结构是每3秒休眠一次。。。为什么我会超过查询限制?谢谢你的帮助
import pandas as pd
import requests
import logging
import time
logger = logging.getLogger("root")
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
API_KEY = my_key #using my API key
BACKOFF_TIME = 5
output_filename = 'result.csv'
input_filename = 'input.csv'
address_column_name = "Address"
RETURN_FULL_RESULTS = False
data = pd.read_csv(input_filename, encoding='utf8')
if address_column_name not in data.columns:
raise ValueError("Missing Address column in input data")
addresses = data[address_column_name].tolist()
def get_google_results(address, api_key=my_key, return_full_response=False):
geocode_url = "https://maps.googleapis.com/maps/api/geocode/json?address={}".format(address)
if api_key is not None:
geocode_url = geocode_url + "&key={}".format(api_key)
results = requests.get(geocode_url)
results = results.json()
if len(results['results']) == 0:
output = {
"formatted_address" : None,
"latitude": None,
"longitude": None,
"accuracy": None,
"google_place_id": None,
"type": None,
"postcode": None
}
else:
answer = results['results'][0]
output = {
"formatted_address" : answer.get('formatted_address'),
"latitude": answer.get('geometry').get('location').get('lat'),
"longitude": answer.get('geometry').get('location').get('lng'),
"accuracy": answer.get('geometry').get('location_type'),
"google_place_id": answer.get("place_id"),
"type": ",".join(answer.get('types')),
"postcode": ",".join([x['long_name'] for x in answer.get('address_components')
if 'postal_code' in x.get('types')])
}
output['input_string'] = address
output['number_of_results'] = len(results['results'])
output['status'] = results.get('status')
if return_full_response is True:
output['response'] = results
return output
results = []
for address in addresses:
geocoded = False
while geocoded is not True:
try:
geocode_result = get_google_results(address, API_KEY,
return_full_response=RETURN_FULL_RESULTS)
time.sleep(5)
except Exception as e:
logger.exception(e)
logger.error("Major error with {}".format(address))
logger.error("Skipping!")
geocoded = True
if geocode_result['status'] == 'OVER_QUERY_LIMIT':
logger.info("Hit Query Limit! Backing off for a bit.")
time.sleep(BACKOFF_TIME * 60) # sleep
geocoded = False
else:
if geocode_result['status'] != 'OK':
logger.warning("Error geocoding {}: {}".format(address, geocode_result['status']))
logger.debug("Geocoded: {}: {}".format(address, geocode_result['status']))
results.append(geocode_result)
geocoded = True
if len(results) % 100 == 0:
logger.info("Completed {} of {} address".format(len(results), len(addresses)))
if len(results) % 50 == 0:
pd.DataFrame(results).to_csv("{}_bak".format(output_filename))
logger.info("Finished geocoding all addresses")
pd.DataFrame(results).to_csv(output_filename, encoding='utf8')