我們這里使用的是baidumap的高精度IP定位API的接口,具體說明可以參考baidu提供的文檔
使用前需要申請一個AK密鑰類似於Kgcx......................xxl8w的樣式
http://api.map.baidu.com/highacciploc/v1?qcip=220.xxx.xx.113&qterm=pc&ak=Kgcx......................xxl8w&coord=bd09ll&extensions=1
這是我的訪問的URL,其中參數可以參考文檔給出的定義
下圖是訪問到的網頁
這里我主要寫一個腳本,對抓到的html進行格式化
1 # -*- coding: utf-8 -*- 2 """ 3 Created on Wed Oct 12 12:40:24 2016 4 5 @author: Administrator 6 """ 7 import requests 8 import json 9 10 URL = 'http://api.map.baidu.com/highacciploc/v1?qcip=110.xxxx.31.6&qterm=pc&ak=Kgcx0pNF6.....................xl8w&coord=bd09ll&extensions=1' 11 12 def get_html(url): 13 headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1581.2 Safari/537.36'} 14 text = requests.get(url,headers=headers).text 15 return text 16 17 def formated_html(text): 18 jo = json.loads(text) #或者直接用eval函數將text轉成dict 19 business = jo['content']['business'] 20 for k,v in jo['content']['address_component'].items(): 21 print k,':', v 22 print 'formatted_address',':',jo['content']['formatted_address'] 23 print 'business',':',jo['content']['business'] 24 print 'location',':',jo['content']['location'] 25 26 if __name__ == '__main__': 27 text = get_html(URL) 28 formated_html(text) 29 30
下面是格式化后的結果:
但是官方給出了注意事項:成功率:綜合定位成功率 65% ,精度:90% 誤差 80m 以內;95% 誤差 350m