1. 程式人生 > >突然發現百度的API越來越好用了,簡單使用百度API精準定位IP地址。附Python程式碼

突然發現百度的API越來越好用了,簡單使用百度API精準定位IP地址。附Python程式碼

2016年11月27日08:13:13

API服務地址:
http://lbsyun.baidu.com/index.php?title=webapi/high-acc-ip

使用方法:
第一步,申請金鑰(AK) ,作為訪問服務的依據;
第二步,拼寫傳送HTTP/HTTPS請求的URL,注意需使用第一步申請的AK;
第三步,接收HTTP/HTTPS請求返回的資料(JSON/JSONP格式)

服務地址:
 http://api.map.baidu.com/highacciploc/v1
 https://api.map.baidu.com/highacciploc/v1

注意:申請開發者認證的時候,使用163郵箱認證失敗,不知道是為什麼(2016年11月27日08:23:32)

測試地址:
http://api.map.baidu.com/highacciploc/v1?qcip=139.214.254.47&qterm=pc&ak=naW5VG6u5TELUZ2stDslQPsYirKoIOFz&coord=bd09ll&extensions=3

反饋結果:
{"content":{"location":{"lat":43.862307,"lng":125.332422},"locid":"fb823867162b178b3e180993e4638d41","radius":2060684,"confidence":0.2,"address_component":{"country":"中國","province":"吉林省","city":"長春市","district":"南關區","street":"人民大街","street_number":"5766號","admin_area_code":220102},"formatted_address":"吉林省長春市南關區人民大街5766號","business":"人民大街,南嶺,南湖公園"},"result":{"error":161,"loc_time":"2016-11-27 08:36:41"}}


# -*- coding: utf-8 -*-
import requests
import json

s = requests.session()
url = 'http://api.map.baidu.com/highacciploc/v1?qcip=139.214.254.47&qterm=pc&ak=YourKey&coord=bd09ll&extensions=3'
header = {'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0'}
response = s.get(url, headers = header, timeout = 20)
print(response.text)

json = json.loads(response.text)

print('位置:'+str(json['content']['formatted_address']))
print('商圈:'+str(json['content']['business']))
print('經度:'+str(json['content']['location']['lat']))
print('維度:'+str(json['content']['location']['lng']))
print('準確度:'+str(json['content']['confidence']))

#練習時間:2016年11月27日10:54:36
#1、注意使用字元編碼。
#2、注意修改requests請求header中的User-Agent。
#3、json返回的資料可能是int或者float等,列印時注意字元轉換。
#4、替換API中你需要查詢的IP地址和你的Key字串。



=====================
輸入結果
{"content":{"location":{"lat":43.862307,"lng":125.332422},"locid":"180d8c65fa25fefaf6eb9ae6481e1f4c","radius":2060684,"confidence":0.2,"address_component":{"country":"中國","province":"吉林省","city":"長春市","district":"南關區","street":"人民大街","street_number":"5766號","admin_area_code":220102},"formatted_address":"吉林省長春市南關區人民大街5766號","business":"人民大街,南嶺,南湖公園"},"result":{"error":161,"loc_time":"2016-11-27 10:45:03"}}

位置:吉林省長春市南關區人民大街5766號
商圈:人民大街,南嶺,南湖公園
經度:43.862307
維度:125.332422
準確度:0.2