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.源代碼

 

 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代碼

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM