赞
踩
在开放平台里面注册后,打开我的应用,并创建应用,回到首页点击开发支持里的web服务API,里面有大量的说明文档和类型接口,开发者可以根据实际情况选择,操作上基本上大同小异。
import requests # 导入requests
import re # 正则等下会应用到提参
if __name__ == '__main__':
url = 'https://restapi.amap.com/v4/direction/bicycling?'
params = {
'key': 'c26090cfad60515aa67024f2c4c644db', # 应用key值
'origin': 'origin', # 输入起点经纬度
'destination': 'destination', # 输入终点经纬度
'output': 'json'# 输出格数 可以是xml 这里为了方便直接json
}
r = requests.get(url=url, params=params).text
# get请求后面除了带上url,还有参数集params
pycharm输出:
json如下:
是不是很乱,下面我们来清洗数据
代码如下(示例):
"destination":"116.465302,40.004717","origin":"116.481028,39.989643" "paths":[] "orientation":"" . . . @等等,包括方向,距离,道路名称的都是需要的吧 @这里直接用上正则来匹配它们,为什么不用xpath,bs4?后者对于json格式来说操作性不灵活,所以这里直接上手正则 distance_data = '"distance":(.*?),' duration_data = '"duration":(.*?),' action_data = '"action":"(.*?)"' instruction_data = '"instruction":"(.*?)"' orientation_data = '"orientation":"(.*?)"' polyline_data = '"polyline":"(.*?)"' road_data = '"instruction":"(.*?)"' steps_data = '"steps":(.*?),"errcode"' distance_list = re.findall(distance_data, r) duration_list = re.findall(duration_data, r) action_list = action_debug instruction_list = re.findall(instruction_data, r) orientation_list = re.findall(orientation_data, r) polyline_list = re.findall(polyline_data, r) road_list = re.findall(road_data, r)
n = len(action_debug)
print('一共' + str(n) + '步')
for a in range(n):
print("第" + str(a) + '步')
distance_list = re.findall(distance_data, r)[a]
duration_list = re.findall(duration_data, r)[a]
action_list = action_debug[a]
instruction_list = re.findall(instruction_data, r)[a]
orientation_list = re.findall(orientation_data, r)[a]
polyline_list = re.findall(polyline_data, r)[a]
road_list = re.findall(road_data, r)[a]
给一个循环让输出值按一步步来实现,更加直观。
origin = input('inter a origin: ' + str(''))
destination = input('inter a destination: ' + str(''))
'origin': origin,
'destination': destination,
经纬度可以自由填写
import requests import re distance_data = '"distance":(.*?),' duration_data = '"duration":(.*?),' action_data = '"action":"(.*?)"' instruction_data = '"instruction":"(.*?)"' orientation_data = '"orientation":"(.*?)"' polyline_data = '"polyline":"(.*?)"' road_data = '"instruction":"(.*?)"' steps_data = '"steps":(.*?),"errcode"' origin = input('inter a origin: ' + str('')) destination = input('inter a destination: ' + str('')) if __name__ == '__main__': url = 'https://restapi.amap.com/v4/direction/bicycling?' params = { 'key': 'c26090cfad60515aa67024f2c4c644db', 'origin': origin, 'destination': destination, 'output': 'json'} r = requests.get(url=url, params=params).text print(r) steps_list = re.findall(steps_data, r)[0] action_debug = re.findall(action_data, r) str_list = [''] n = len(action_debug) print('一共' + str(n) + '步') for i in range(n): if action_debug[i] in str_list: action_debug[i] = 'none' print(action_debug) for a in range(n): print("第" + str(a) + '步') distance_list = re.findall(distance_data, r)[a] duration_list = re.findall(duration_data, r)[a] action_list = action_debug[a] instruction_list = re.findall(instruction_data, r)[a] orientation_list = re.findall(orientation_data, r)[a] polyline_list = re.findall(polyline_data, r)[a] road_list = re.findall(road_data, r)[a] print(distance_list, duration_list, action_list, instruction_list, orientation_list, polyline_list, road_list)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。