python实现基于百度路径规划接口的坐标对获取两点驾车距离的计算

今天为大家介绍一种通过python实现坐标对间距离数据的获取方法。接口采用百度开发的路径规划接口。

  1.调用接口:

接口:(传入起点坐标串,结束坐标串;ak值需要注册百度开发者) 接口详细说明

http://api.map.baidu.com/direction/v2/driving?origin=40.01116,116.339303&destination=39.936404,116.452562&ak=您的AK  //GET请求

  2.AK值获取:

 注册成为开发者后需要添加应用,添加服务端应用勾选路径规划选项,其ak值才能调取该接口,不然将出现‘204,app拒绝服务’。

  

  3.实现思路

  1. 坐标对数据集(Y.txt)中获取坐标对;----->(x1,y1/x2,y2) ----->
    30.552413,114.267227/30.564768,114.235462/4758
  2. 日志记录(logo.txt)记录抓取过的记录序号(开始默认为-1,其后不再更改记录),用于断点续爬;----->(-1、0、1.....)
  3. 结果集(save.txt)用于记录数据;----->(x1,y1/x2,y2/s)----->
    30.552413,114.267227/30.564768,114.235462/4758
  4. 文件目录结构如下图:

  4.源代码

扫描二维码关注公众号,回复: 9362773 查看本文章
 1 # coding=utf-8
 2 import requests
 3 import re
 4 import os
 5 from time import sleep
 6 #访问url,返回数据JSON
 7 def get_JSON(startStr,endStr,key):
 8     sleep(0.5)
 9     url='http://api.map.baidu.com/direction/v2/driving?origin='+startStr+'&destination='+endStr+'&ak='+key
10     # print (url)
11     headers = {
12         'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36',
13         'referer':"http://www.baidu.com",#伪造一个访问来源
14     }
15     poi_JSON = requests.get(url, headers=headers).json()
16     return poi_JSON
17 
18 
19 #发送请求获取json数据
20 def jiexi_Json(resJSON):
21     status=resJSON['status']
22     if status==0:# 请求到数据
23         # print('数据请求成功')
24         distance=resJSON['result']['routes'][0]['distance']
25         print('路径计算完成',distance)
26         return distance
27     else:
28         if  status==1:
29             print('服务内部错误')
30         elif status==2:
31             print('参数无效')
32         elif status==2001:
33             print('无骑行路线')
34         elif status==240:
35             print('ak值注册不正确,未包含路径规划服务')
36         else:# 额度没有了
37             print('额度不够了!')
38 
39 # 读取坐标对
40 def read_File(filePath):
41     file = open(filePath)
42     logoFilePath='logo.txt'
43     logoFile=open(logoFilePath)
44     for oldIndex in logoFile:# 读取日志
45         for index, coorItemStr in enumerate(file): # 读取坐标对
46             if index>int(oldIndex):
47                 print('正在请求第',str(index+1),'条数据')
48                 coorArr=coorItemStr.rstrip("\n").split('/')
49                 startStr=coorArr[0].split(',')[1]+','+coorArr[0].split(',')[0]
50                 endStr=coorArr[1].split(',')[1]+','+coorArr[1].split(',')[0]
51                 key='你自己的百度密钥' # 百度密钥
52                 #发送距离量算请求
53                 poi_JSON = get_JSON(startStr,endStr,key)
54                 distance=jiexi_Json(poi_JSON)
55                 saveStr=startStr+'/'+endStr+'/'+str(distance)
56                 saveText('saveY.txt',saveStr,'a+')# 记录结果
57                 saveText(logoFilePath,str(index),'w')# 记录数据请求日志
58             else:
59                 print('',str(index+1),'条数据已经请求完成')
60         print('数据转换已完成!')
61 
62 
63 # 以txt文件格式存储
64 def saveText(filePath,str,type):
65     type=(type if type else "a+")
66     # 打开一个文件
67     fo = open(filePath,type)
68     fo.write(str); #内容写入
69     fo.write('\n')
70     fo.close()# 关闭打开的文件
71 
72 if __name__ == "__main__":
73     read_File('Y.txt')
py代码

  

猜你喜欢

转载自www.cnblogs.com/giserjobs/p/12354932.html