突然发现百度的API越来越好用了,简单使用百度API精准定位IP地址。附Python代码
- # -*- 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