API 的地址是 http://ip.taobao.com/service/getIpInfo.php?ip=你需要查询的IP
{"code":0,"data":{"country":"\u4e2d\u56fd","country_id":"CN","area":"\u897f\u5357","area_id":"500000","region":"\u56db\u5ddd\u7701","region_id":"510000","city":"\u7ef5\u9633\u5e02","city_id":"510700","county":"","county_id":"-1","isp":"\u8054\u901a","isp_id":"100026","ip":"221.10.97.147"}}
上面就是我查询我现在所在的外网地址得出来的
(json格式的)国家 、省(自治区或直辖市)、市(县)、运营商
其中code的值的含义为,0:成功,1:失败。
country:国家
area:地区
region:所在省份
city:所在城市
county:县
isp:运营商
然后 得到的数据是经过 unicode 编码的 然后获取实际的数据需要解码
python 解码方面可以参考这里
http://luchanghong.com/python/2012/07/06/python-encoding-with-unicode-and-gbk-and-utf8.html
然后就根据需要提取内容就好了 写个例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import
urllib
import
json
def
ipInfo(ipaddress):
url
=
'http://ip.taobao.com/service/getIpInfo.php?ip='
+
ipaddress
page
=
urllib.urlopen(url)
data
=
page.read()
jsondata
=
json.loads(data)
if
jsondata[u
'code'
]
=
=
0
:
print
'所在国家:'
+
jsondata[u
'data'
][u
'country'
].encode(
'utf-8'
)
print
'所在地区:'
+
jsondata[u
'data'
][u
'area'
].encode(
'utf-8'
)
print
'所在省份:'
+
jsondata[u
'data'
][u
'region'
].encode(
'utf-8'
)
print
'所在城市:'
+
jsondata[u
'data'
][u
'city'
].encode(
'utf-8'
)
print
'所用运营商:'
+
jsondata[u
'data'
][u
'isp'
].encode(
'utf-8'
)
else
:
print
'查询失败 请检查IP 后再说'
|
只是简单的获取数据 解析数据而已
本文转自 拖鞋崽 51CTO博客,原文链接:http://blog.51cto.com/1992mrwang/1206673