python获得url的顶级域名,pythonurl顶级域名,如下代码topHostP


如下代码topHostPostfix是顶级域名的后缀,get_top_host函数会根据这些后缀组合一个正则表达式来解析顶级域名:

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')def get_top_host(url):    parts = urlparse(url)    host = parts.netloc    extractPattern = r'[^\.]+('+'|'.join([h.replace('.',r'\.') for h in topHostPostfix])+')$'    pattern = re.compile(extractPattern,re.IGNORECASE)    m = pattern.search(host)    return m.group() if m else host

评论关闭