您的位置:首页 > 编程语言 > Python开发

突然发现百度的API越来越好用了,简单使用百度API精准定位IP地址。附Python代码

2016-11-27 16:38 686 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  api 数据 python ip 定位