- #! /usr/bin/env python
- import re,urllib2,sys
- class ip():
- def __init__(self,n):
- self.n=n;
- self.u="http://www.ip138.com/ips138.asp?ip=%s&action=2" %(self.n)
- def run(self):
- ip=re.compile(r"([0-9]{1,3}\\.){3}([0-9]{1,3})")
- if ip.match(self.n):
- self.find(self.u)
- else:
- print "N"
- sys.exit()
- def find(self,u):
- n=urllib2.urlopen(self.u)
- url=re.compile(r'.*?<td align="center">.*?<li>(.*?)</li>.*')
- for i in n:
- ma=url.match(i)
- if ma:
- print ma.group(1).decode("gb2312")
- n.close()
- if __name__=="__main__":
- try:
- n=raw_input("please input ip-->")
- i=ip(n)
- i.run()
- except Exception as err:
- print err
- #该片段来自于http://www.codesnippet.cn/detail/080820135031.html
来源: http://www.codesnippet.cn/detail/080820135031.html