1、经过淘宝API接口php
接口说明:python
1. 请求接口(GET):json
http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串]api
2. 响应信息:ide
(json格式的)国家 、省(自治区或直辖市)、市(县)、运营商url
3. 返回数据格式:code
{"code":0,"data":{"ip":"210.75.225.254","country":"\u4e2d\u56fd","area":"\u534e\u5317",orm
"region":"\u5317\u4eac\u5e02","city":"\u5317\u4eac\u5e02","county":"","isp":"\u7535\u4fe1",接口
"country_id":"86","area_id":"100000","region_id":"110000","city_id":"110000",ip
"county_id":"-1","isp_id":"100017"}}
其中code的值的含义为,0:成功,1:失败。
#!/usr/bin/python #coding:utf-8 import urllib2 import json ip=raw_input("请输入要查询的IP:") apiurl = "http://ip.taobao.com/service/getIpInfo.php?ip=%s" % ip content = urllib2.urlopen(apiurl).read() data = json.loads(content)['data'] code = json.loads(content)['code'] if code == 0: print "\nIP: %s Form: %s%s%s ISP: %s\n" % (data['ip'], data['country'], data['region'], data['city'], data['isp']) else: print data
2、经过ip138
#!/usr/bin/python #coding:utf-8 import urllib2 import re p = re.compile(r'.*<li>(.*)</li><li>(.*)</li>.*') p_ip = re.compile(r'^(?:(?:[1-9]?[0-9]|1[0-9]{2}|2(?:[0-4][0-9]|5[0-5]))\.){3}(?:[1-9]?[0-9]|1[0-9]{2}|2(?:[0-4][0-9]|5[0-5]))$') ip=raw_input("请输入要查询的IP:") if p_ip.search(ip): apiurl = "http://www.ip138.com/ips138.asp?ip=%s" % ip content = urllib2.urlopen(apiurl).read() ipline=p.search(content).group() print p.sub(r'\1\n\2',ipline).decode('gbk') else: print "Error:请输入正确的IP"
其实只是用来练习写脚本而已,平时若是要查询一个IP,直接打开百度搜索就出来了。