python 利用淘宝IP库 查询IP归属地

#coding:utf-8
from django.test import TestCase

import json
import urllib

ip = "114.114.114.114"
url = "http://ip.taobao.com/service/getIpInfo.php?ip="+ ip

#返回数据
jsondata = json.loads(urllib.urlopen(url).read())
print jsondata

#省份
region = jsondata["data"]["region"]
#城市
city = jsondata["data"]["city"]
#运营商
isp = jsondata["data"]["isp"]
#国家
country = jsondata["data"]["country"]

print region
print city
print isp
print country

 结果:

{u'code': 0, u'data': {u'city': u'\u5357\u4eac', u'area_id': u'', u'county_id': u'xx', u'region_id': u'320000', u'area': u'', u'city_id': u'320100', u'ip': u'114.114.114.114', u'region': u'\u6c5f\u82cf', u'isp': u'XX', u'country_id': u'CN', u'county': u'XX', u'isp_id': u'xx', u'country': u'\u4e2d\u56fd'}}
江苏
南京
XX
中国

猜你喜欢

转载自www.cnblogs.com/fanhua999/p/8989834.html