首页 > 代码库 > IP地址查询

IP地址查询

 

# -*- coding: UTF-8 -*-

ip = 61.158.147.194

import urllib,urllib2,cookielib,sys,cjson
reload(sys)
sys.setdefaultencoding(utf-8)

url = http://ip.taobao.com/service/getIpInfo.php? #定义接口地址
headers = {
   User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
}
headers = {User-agent:Mozilla/5.0} #---OK
url_args = urllib.urlencode({"ip":ip}) 
urls = %s%s %(url,url_args)
req = urllib2.Request(url=urls,headers=headers) #需要添加一个header,否则会提示403forbidden
res = urllib2.urlopen(req).read() #返回:aabb00


res_d = cjson.decode(res)[data]
# res_d = json.loads(res,strict=False,encoding=‘utf-8‘)
print res_d[ucountry],res_d[uregion],res_d[ucity],res_d[uisp]
#返回值:中国 河南省 开封市 联通

 

IP地址查询