實用技巧:如何通過IP位址進行精準定位

在甲方工作的朋友可能會遇到這樣的問題,服務器或者系統經常被掃描,通過IP地址我們只能查到某一個市級城市,如下圖:

實用技巧:如何通過IP地址進行精準定位

當我們想具體到街道甚至門牌號,該怎麼辦???

偶然間發現百度地圖有高精度IP定位API的接口,通過該接口我們可以通過IP地址定位到具體的地理位置,甚至能精確到門牌號及周圍的標誌性建築。該接口的說明地址為:

實用技巧:如何通過IP地址進行精準定位

http://lbsyun.baidu.com/index.php?title=webapi/high-acc-ip

若想要使用該接口進行查詢,必須先申請一個密鑰(AK),如下圖:

實用技巧:如何通過IP地址進行精準定位

申請過程就不進行說明了。API的接口參數說明和返回參數說明也不過多的介紹,大家可以看一看。因為我想返回基礎定位結果+地址信息+POI信息,所以我將請求參數extensions的值設置為3。一次完整的http請求為:

實用技巧:如何通過IP地址進行精準定位

v1?qcip=183.55.116.90&qterm=pc&ak=

“你的 密鑰(AK)” &coord=bd09ll&extensions=3 。請求結果如下圖:

實用技巧:如何通過IP地址進行精準定位

結果為json格式數據:

{"content":{"location":{"lat":23.06588,"lng":115.404586},"locid":"925a2a9e3ac5be1cf003afd23c344ab3","radius":30,"confidence":0.5,"address_component":{"country":"中國","province":"廣東省","city":"汕尾市","district":"海豐縣","street":"新平路","street_number":"","admin_area_code":441521},"formatted_address":"廣東省汕尾市海豐縣新平路","business":"公平"},"result":{"error":161,"loc_time":"2016-10-19 21:53:28"}}我們需要的字段為:content字段裡面的formatted_address。當然我們也可以將location裡面的經度和緯度提取出來從而顯示在地圖上面。有的IP地址會返回pois數據,比如:183.55.116.95。返回參數如下:

{"content":{"location":{"lat":23.082367,"lng":115.466276},"locid":"3fb96555906fff3100ff21119142ccd5","radius":30,"confidence":1.0,"address_component":{"country":"中國","province":"廣東省","city":"汕尾市","district":"海豐縣","street":"S335","street_number":"","admin_area_code":441521},"formatted_address":"廣東省汕尾市海豐縣S335","pois":[{"name":"雙墩村","address":"汕尾市海豐縣三三五省道","tag":"行政地標;村莊","location":{"lat":23.082422,"lng":115.465348},"uid":"18010998377147269119"},{"name":"雙墩村委會","address":"汕尾市海豐縣","tag":"政府機構;各級政府","location":{"lat":23.083394,"lng":115.465914},"uid":"17661602237861855231"},{"name":"長聯塘尾","address":"汕尾市海豐縣","tag":"行政地標;村莊","location":{"lat":23.081358,"lng":115.467315},"uid":"18010998372852301823"},{"name":"雙墩小學","address":"335省道附近","tag":"教育培訓;小學","location":{"lat":23.083336,"lng":115.465061},"uid":"17661601958688980991"},{"name":"大溪頭","address":"汕尾市海豐縣","tag":"行政地標;村莊","location":{"lat":23.090326,"lng":115.465995},"uid":"18010998368557334527"}],"location_description":"雙墩村東104米"},"result":{"error":161,"loc_time":"2016-10-19 22:03:31"}}此時我們可以把pois字段也提取出來,值得注意的是pois為數組,我們可以遍歷數組數據。

通過上面的分析,用python簡單的寫了一個腳本,具體代碼如下:

# -*- coding:utf-8 -*-

# author:allen權

import sys

import urllib2

import json

def get_ip_information(ip):

url='http://api.map.baidu.com/highacciploc/v1?qcip='+ip+'&qterm=pc&ak='你的密鑰(AK)'&coord=bd09ll&extensions=3'

poiss=''

request = urllib2.Request(url)

page = urllib2.urlopen(request, timeout=10)

data_json = page.read()

data_dic = json.loads(data_json)

if(data_dic.has_key("content")):

content=data_dic["content"]

address_component=content["address_component"]

formatted_address=content["formatted_address"]

print "該IP地址的具體位置為:"

print address_component["country"]

print formatted_address

if (content.has_key("pois")):

print "該IP地址附近POI信息如下:"

pois = content["pois"]

for indexin range(len(pois)):

pois_name = pois[index]["name"]

pois_address = pois[index]["address"]

print pois_name, pois_address

else:

print 'IP地址定位失敗!!!'

if __name__ == '__main__':

get_ip_information('183.55.116.95')大家把腳本上面的參數ak值改為自己的密鑰即可。測試截圖如下:

實用技巧:如何通過IP地址進行精準定位

再放一張自己IP的測試截圖:(不是我本人的ip。2223333)

實用技巧:如何通過IP地址進行精準定位


分享到:


相關文章: