Shu*_*m R 5 python google-maps-api-3 python-3.x python-requests google-distancematrix-api
我正在尝试开发一些涉及在两个地理位置之间找到距离的东西.
源和目标有很多这样的组合,我在循环中传入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
Run Code Online (Sandbox Code Playgroud)
这只是我代码的一部分.
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',))
Run Code Online (Sandbox Code Playgroud)
我也尝试使用会话而不是请求,但同样的错误即将来临.
我想知道造成这个问题的原因是什么?
如何在不使用的情况下更好地纠正它try-catch.
我认为主要问题源于代码格式不正确。按照现在的设置方式,我不相信您能够阻止该错误。
嵌套 for 循环
for i in geocodes:
for j in key_list:
Run Code Online (Sandbox Code Playgroud)
geocodes是包含起点和目的地坐标的列表列表,并key_lists包含所有 api 密钥。这里的问题是geocodes第一个for循环。本质上,您中的每个键key_list都用于查找每个坐标对的距离矩阵。这是浪费api资源。
不同 api 密钥的使用
api_url = base_url + origin_part + destination_part + key
Run Code Online (Sandbox Code Playgroud)
这行代码属于您的嵌套for循环,并生成您的 api url。这里的问题是它永远不会改变。最后一个元素是key. 既然你说这只是代码片段,我们不知道这key意味着什么,但我假设它是在代码前面设置的静态 api 密钥。当它实际上应该是j相反的时候,这会切换按键。
钥匙开关错误
if json_response["status"] == "OVER_QUERY_LIMIT":
j+=1
i-=1
Run Code Online (Sandbox Code Playgroud)
这行代码并不完全按照您的想法工作。它不会转移到jfor 循环中的下一个,并返回到i. 这只会从它们的值中添加或减去 1,并且因为没有ints或floats最终会抛出错误。
相反,我建议您将代码更改为此。
base_url = 'https://maps.googleapis.com/maps/api/distancematrix/json?'
key_list = [****,****,....] #google key list
index,atEnd = 0,False
for coords in geocodes:
origin_part = 'origins=' + coords[0] + "," + coords[1]
destination_part = '&destinations=' + coords[2] + "," + coords[3]
api_url = base_url + origin_part + destination_part + key_list[index]
json_response = requests.get(api_url,timeout=10).json()
#Check if your over your daily limit, if so try the next key
while json_response['status'] == 'OVER_QUERY_LIMIT':
index += 1
#if all keys used to max quotas, exit
if index == len(key_list):
atEnd = True
break
api_url = base_url + origin_part + destination_part + key_list[index]
json_response = requests.get(api_url, timeout=10).json()
print json_response
#Exit main loop if all quotas reached
if atEnd == True:
print'All of your keys quotas have been used, try again tomorrow'
break
Run Code Online (Sandbox Code Playgroud)
请注意,我不太熟悉地图 API 的合法用途。但是,我非常确定您的行为违反了 Google 的条款和条件,因为您使用多个帐户超出了每日使用限制。
我建议在谷歌开发者控制台上进行免费试用。他们向您提供 300 美元的信用额度,以便您使用他们的服务以超出报价。