目前我的流程如下:
我收到了Google Directions API的JSON回复。我的代码在99%的情况下都可以正常工作,但如果我没有按预期收到JSON数组,则会失败。由于我在循环之后将响应批量写入csv,如果发生错误,我将丢失所有结果。
代码如下:
import requests
import csv
import json
import urlparse
import hashlib
import base64
import hmac
import sys
import time
from pprint import pprint
url_raw = 'https://maps.googleapis.com/maps/api/directions/json'
Private_Key = ''
client = ''
decodedkey = base64.urlsafe_b64decode(Private_Key)
with open('./Origins.csv', 'rU') as csvfile:
reader = csv.DictReader(csvfile)
origincoords = ['{Y},{X}'.format(**row) for row in reader]
with open('./Destinations.csv', 'rU') as csvfile:
reader = csv.DictReader(csvfile)
destinationcoords = ['{Y},{X}'.format(**row) for row in reader]
results=[]
session = requests.session()
for origin, destination in zip(origincoords, destinationcoords):
params ={'origin': origin, 'destination': destination, 'client': client}
request = requests.Request('GET', url_raw, params=params).prepare()
parsed_url = urlparse.urlparse(request.url)
Signature = hmac.new(decodedkey, '{}?{}'.format(parsed_url.path, parsed_url.query), hashlib.sha1).digest()
request.prepare_url(request.url, {'signature': base64.urlsafe_b64encode(Signature)})
response = session.send(request)
directions = response.json()
time.sleep(0.0)
results.append(directions)
pprint(results)
output = open('duration_distance_results.csv', 'w+')
writer = csv.DictWriter(output, delimiter=',', fieldnames=['duration(s)', 'distance(m)'])
writer.writeheader()
for route in results:
for leg in route['routes'][0]['legs']:
params = {
"duration(s)": leg['duration']['value'],
"distance(m)": leg['distance']['value'],
}
print(params)
writer.writerow(params)
如果我没有在预期的数组中得到JSON响应,我会得到一个超出范围错误的列表索引,并且通过这个松散的方式到目前为止已经完成了。
理想情况下,我认为如果我在循环中写入csv会更加健壮,因为每次收到结果,而不是在收到所有结果后执行此操作。或者,正如我所尝试的那样,使用if语句 -
我尝试过if语句,如果存在值,则写入值,如果没有,则写入错误。但是,我收到以下错误" NameError:name' leg'没有定义"
如果路线中的腿['路线'] [0] ['腿'] ==值: 对于结果中的路线: 路线中的腿['路线'] [0] ['腿']: params = { "持续时间":腿['持续时间'] ['价值'], "距离(m)&#34 ;:腿['距离'] ['值'], } 打印(PARAMS) writer.writerow(PARAMS)
否则: 对于结果的价值: error = { "持续时间":'错误', "距离(m)":'错误', } 打印(错误) writer.writerow(误差)
我尝试了一个if语句,查看从Google返回的状态消息。但是,对于错误"对于结果中的error_message ['结果'] [0] [' error_message'],此操作失败: TypeError:list indices必须是整数,而不是str"
表示结果中的error_message ['结果'] [0] [' error_message']: params = {" error_message":error_message}
if error_message == value:
for route in results:
for leg in route['routes'][0]['legs']:
params = {
"duration(s)": leg['duration']['value'],
"distance(m)": leg['distance']['value'],
}
print(params)
writer.writerow(params)
else:
for value in results:
error = {
"duration(s)": 'error',
"distance(m)": 'error',
}
print(error)
writer.writerow(error)
pprint(结果)
很明显,我在这里慢慢学习。关于处理错误的最佳方法的评论将非常感激。
答案 0 :(得分:0)
要做的第一件明显的事情是检查response
对象的HTTP状态代码。它不是Google API文档的明确部分,但显然不是200(" Found")HTTP状态代码意味着您遇到了问题而且您甚至无法获得任何有用的信息。回应的身体。 HTTP响应代码记录在HTTP RFC中。
然后,如果您阅读了API的文档(https://developers.google.com/maps/documentation/directions/),您就会发现返回的json dict具有“'状态'键,也记录了哪些值。所以下一个显而易见的事情是检查这个状态代码,并且表现得恰当,即:
for result in results:
if result["status"] == "OK":
for leg in result['routes'][0]['legs']:
params = {
"duration(s)": leg['duration']['value'],
"distance(m)": leg['distance']['value'],
}
print(params)
writer.writerow(params)
else:
# not OK, let's check if we have an explicit error message:
if "error_message" in result:
print result["error_message"]
# handle the error case...