#python 2.x的写法
#coding = utf-8
import urllib2
import json
def ip2city(ip):
apiurl = "http://ip.taobao.com/service/getIpInfo.php?ip=%s" % ip
content = urllib2.urlopen(apiurl).read()
data = json.loads(content)['data']
print data
code = json.loads(content)['code']
if code == 0:
print data["country"],data["region"],data["city"],data["ip"]
#python 3.x的写法:
import urllib.request
import json
def ip2city(ip):
apiurl = "http://ip.taobao.com/service/getIpInfo.php?ip=%s" % ip
content = urllib.request.urlopen(apiurl).read()
data = json.loads(content)['data']
code = json.loads(content)['code']
if code == 0:
return data["country"], data["region"],data["city"]