自己用python3实现的方法:
import re
from urllib import parse
#from urlparse import urlparse
topHostPostfix = (
'.com','.la','.io','.co','.info','.net','.org','.me','.mobi',
'.us','.biz','.xxx','.ca','.co.jp','.com.cn','.net.cn',
'.org.cn','.mx','.tv','.ws','.ag','.com.ag','.net.ag',
'.org.ag','.am','.asia','.at','.be','.com.br','.net.br',
'.bz','.com.bz','.net.bz','.cc','.com.co','.net.co',
'.nom.co','.de','.es','.com.es','.nom.es','.org.es',
'.eu','.fm','.fr','.gs','.in','.co.in','.firm.in','.gen.in',
'.ind.in','.net.in','.org.in','.it','.jobs','.jp','.ms',
'.com.mx','.nl','.nu','.co.nz','.net.nz','.org.nz',
'.se','.tc','.tk','.tw','.com.tw','.idv.tw','.org.tw',
'.hk','.co.uk','.me.uk','.org.uk','.vg', ".com.hk")
urls = open("f://1.txt") #从1.txt中提取urls
for url in urls:
regx = r'[^\.]+('+'|'.join([h.replace('.',r'\.') for h in topHostPostfix])+')$'
pattern = re.compile(regx,re.IGNORECASE)
parts = parse.urlparse(url)
host = parts.netloc
m = pattern.search(host)
res = m.group() if m else host
print ("unkonw") if not res else res
print(res)
file_name = "f:/2.txt" #提取的域名写入2.txt
file_open = open(file_name,"a")
file_open.write(res+"\n")
file_open.close()
更多python2相关方法参见:
https://yq.aliyun.com/ziliao/143478