我有一个元组列表,我想迭代并通过python请求模块发送一个http请求。
api_schedule = [
{'destination': (51.490527, 0.267840), 'id': 'trial_a', 'origin': (51.430732, 0.239308)},
{'destination': (51.429488, 0.239060), 'id': 'trial_b', 'origin': (51.490518, 0.267578)}]
原点和目标坐标很难正确格式化。 Google Directions API期望lat,lng
格式为lat
,lng
为float
我遍历词典列表,如下所示:
for pair in api_schedule:
departure_time = time.time()
origin_lat = str(pair['origin'][0])
origin_lng = "," + str(pair['origin'][1])
origin = origin_lat + origin_lng
destination_lat = str(pair['destination'][0])
destination_lng = "," + str(pair['destination'][1])
destination = destination_lat + destination_lng
params = {
"origin" : origin,
"destination" : destination
"key" : api_Key,
"departure_time" : departure_time,
"mode" : mode
}
request = requests.Request('GET', url_raw, params=params).prepare()
print request.url
但是,这会返回%2C
代替,
:
https://maps.googleapis.com/maps/api/directions/json?origin=51.430732%2C0.239308&destination=51.490527&destination=0.26784
如何迭代这样的元组列表并使用请求正确地计算参数?
当我尝试没有上述操作时,请求将原始数据和目标数据视为列表,从而创建两个参数:
https://maps.googleapis.com/maps/api/directions/json?origin=51.430732&origin=0.239308
期望的输出:
https://maps.googleapis.com/maps/api/directions/json?origin=51.430732,0.239308&destination=51.490527,0.26784