高德地图驾车路径规划API,获取两地点之间的驾车里程和时间

高德地图驾车路径规划API,获取两地点之间的驾车里程和时间

import pandas as pd
import requests
import json

def get_dis_tm(origin, destination):
    url = 'https://restapi.amap.com/v3/direction/driving?'
    key = 'xxxx' #这里就是需要去高德开放平台去申请key,请在xxxx位置填写
    link = '{}origin={}&destination={}&key={}'.format(url, origin ,destination , key)
    response = requests.get(link)
    dis, tm = 999999, 999999
    if response.status_code == 200:
        results = response.json()
        if results['status'] == '1':
            dis = int(results['route']['paths'][0]['distance'])
            tm = int(results['route']['paths'][0]['duration'])
        else:
            print(link)
    return dis, tm


#北京和上海的经纬度
beijing = '114.049085,22.556316'
shanghai = '121.473658,31.230378'
get_dis_tm(beijing,shanghai)

(1454483, 53517)

如果没有爬取成功,会输出999999

高德开放平台key获取

高德地图驾车路径规划API,获取两地点之间的驾车里程和时间_第1张图片

高德地图驾车路径规划API,获取两地点之间的驾车里程和时间_第2张图片

你可能感兴趣的:(1024程序员节)