一、开通接口
IP地址归属地查询服务使用聚合数据提供的免费接口,每天可以100次免费调用。可以通过 /docs/api/id/1 注册及开通。
二、请求接口
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib, urllib2, sys, json
reload(sys)
sys.setdefaultencoding('utf-8')
url = '/ip/ipNew'
params = {
"ip": "58.215.15.114", # 查询的IP地址
"key": "******", # 您申请的接口API接口请求Key
}
querys = urllib.urlencode(params)
request = urllib2.Request(url, data=querys)
response = urllib2.urlopen(request)
content = response.read()
if (content):
try:
result = json.loads(content)
error_code = result['error_code']
if (error_code == 0):
country = result['result']['Country']
province = result['result']['Province']
city = result['result']['City']
isp = result['result']['Isp']
print("国家:%s\n省份:%s\n城市:%s\n运营商:%s" % (country, province, city, isp))
else:
print("请求失败:%s %s" % (result['error_code'], result['reason']))
except Exception as e:
print("解析结果异常:%s" % e)
else:
# 可能网络异常等问题,无法获取返回内容,请求异常
print("请求异常")
三、请求结果