Skip to content

Instantly share code, notes, and snippets.

@013231
Created July 21, 2015 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 013231/ddddb6583d337837cdfc to your computer and use it in GitHub Desktop.
Save 013231/ddddb6583d337837cdfc to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from itertools import combinations
import json
import requests
locations = {
'长江南路站',
'浦东机场',
'起点单车世纪公园店',
'南汇嘴观海公园',
'东方明珠',
'虹桥火车站',
'复旦大学江湾校区',
}
def createReqUrl(ori, dst):
template = 'http://api.map.baidu.com/direction/v1?origin={}&destination={}&origin_region=上海&destination_region=上海&output=json&ak=FiEvtaywX9cZpW1LdWAf14UM'
return template.format(ori, dst)
def getDistance(ori, dst):
url = createReqUrl(ori, dst)
resp = requests.get(url).text
jsonResp = json.loads(resp)
if jsonResp['type'] == 2:
distance = jsonResp['result']['routes'][0]['distance']
else:
distance = -1
return distance
if __name__ == '__main__':
for ori, dst in combinations(locations, 2):
distance = getDistance(ori, dst)
if distance > 0:
print('{}\t{}\t{}'.format(ori, dst, distance))
else:
print('{}\t{}\t地址不明'.format(ori, dst))
#Output:
#南汇嘴观海公园 浦东机场 50313
#南汇嘴观海公园 虹桥火车站 90772
#南汇嘴观海公园 起点单车世纪公园店 69830
#南汇嘴观海公园 复旦大学江湾校区 地址不明
#南汇嘴观海公园 东方明珠 76201
#南汇嘴观海公园 长江南路站 83987
#浦东机场 虹桥火车站 59153
#浦东机场 起点单车世纪公园店 36303
#浦东机场 复旦大学江湾校区 地址不明
#浦东机场 东方明珠 42674
#浦东机场 长江南路站 47830
#虹桥火车站 起点单车世纪公园店 29590
#虹桥火车站 复旦大学江湾校区 地址不明
#虹桥火车站 东方明珠 23887
#虹桥火车站 长江南路站 31575
#起点单车世纪公园店 复旦大学江湾校区 地址不明
#起点单车世纪公园店 东方明珠 5663
#起点单车世纪公园店 长江南路站 22097
#复旦大学江湾校区 东方明珠 地址不明
#复旦大学江湾校区 长江南路站 地址不明
#东方明珠 长江南路站 14747
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment