python提取网址URL的域名部分
#!/usr/bin/python # Desc : fetch the domain name of website import re import optparse def get_filename(): p = optparse.OptionParser() p.add_option('-f', action = 'store', dest = 'filename', \ help = 'the directory you want monitoring') opt, args = p.parse_args() if None == opt.filename: p.error('Need a file !') else: filename = opt.filename return filename filename = get_filename() rc = re.compile(r'\w+\.(?:com\.cn|net\.cn|gov\.cn|org\.cn|com|cn|edu|gov|org|cc|net|tk|biz|info|tv|pro|co|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cf|cg|ch|ci|ck|cl|cm|cn|co|cq|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|eh|es|et|ev|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gp|gr|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|ml|mm|mn|mo|mp|mq|mr|ms|mt|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|va|vc|ve|vg|vn|vu|wf|ws|ye|yu|za|zm|zr|zw|me)$', re.I) line_num=1 try: for line in open(filename): print rc.search(line).group() line_num += 1 except AttributeError: print '\nError:\nline_num:%s\ndomain_name:%s' %(str(line_num), line) raise SystemExit