【发布时间】:2018-01-10 18:35:12
【问题描述】:
我正在尝试开发一些涉及查找两个地理位置之间的距离的东西。
有很多这样的源和目标组合,我将它们循环传递给 google distance API。
我的部分代码:
key_list = [****,****,....] #google key list
base_url = 'https://maps.googleapis.com/maps/api/distancematrix/json?'
for i in geocodes:
for j in key_list:
origin_part = 'origins=' + i[0] + "," + i[1]
destination_part = '&destinations=' + i[2] + "," + i[3]
api_url = base_url + origin_part + destination_part + key
json_response = requests.get(api_url,timeout=10).json()
if json_response["status"] == "OVER_QUERY_LIMIT":
j += 1
i -= 1
这只是我代码的一部分。
geocodes 是一个包含子列表的嵌套列表
[[source_latitude,source_longitude,destination_latitude,destination_longitude],....]
代码运行良好一段时间,但一段时间后,它给我一个错误说:
HTTPSConnectionPool(host='maps.googleapis.com', port=443): Max retries exceeded with url: /maps/api/distancematrix/json?origins=xx,xx&destinations=xx,xx&key=xxxxx
(Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f53350c50f0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))
我也尝试使用会话而不是请求,但同样的错误即将到来。
我想知道是什么导致了这个问题?
我怎样才能纠正它,最好不使用try-catch。
【问题讨论】:
-
有多少目的地/来源?请提供一个 minimal reproducible example 来证明问题
-
@geocodezip 大约要计算 10 万个距离,但错误在 1000-2000 行之后随机出现,在计算多少距离后可能会出现错误并没有具体说明。
-
您有完全访问 google maps api 的权限吗?还是您只使用免费版本?
-
检查你是否在谷歌控制台的OVER_QUERY_LIMIT中。
标签: python python-3.x google-maps-api-3 python-requests google-distancematrix-api