实用技巧:如何通过IP地点进行精准定位 8090安适门户
在甲方事情的伴侣可能会遇到这样的问题,处事器或者系统经常被扫描,通过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为数组,我们可以遍历数组数据。
大家把脚本上面的参数ak值改为本身的密钥即可。测试截图如下:
# -*- coding:utf-8 -*-# author:allen权import sysimport urllib2import jsondef 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 index in 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')
温馨提示: 本文由杰米博客推荐,转载请保留链接: https://www.jmwww.net/file/pc/12477.html