适用版本Python2 or Python3:模块python-whois
安装包地址:https://pypi.python.org/pypi/python-whois
教程地址:https://bitbucket.org/richardpenman/pywhois
安装:pip install python-whois
卸载:pip uninstall python-whois
使用方式:
import rec='sdsdaherf=sadasdada sdad123,21312!!!' pattern=re.compile('herf=(.*)') pattern.findall(c) a=re.findall('herf=(.*)', c) a 结果就是 Out[9]: ['sadasdada sdad123,21312!!!']一般我们常见的网址后缀(suffix)是 cn,或者net,或者com,你说的就是域名的后缀列表后面代码就是用dot把域名分隔开,
比如www.baidu.com 被拆分成[ ‘www','baidu','com']
经过for循环后,到com的时候,进入if分支,而前面走的是else分支,所以可以看到sdomain的变化如下:
遇到www, sdomain包含['www']
遇到baidu sdomain被替换为['baidu']
遇到com,走if分支,append,变成 ['baidu','com']
然后join后就变成 baidu.com
不过不知道这么些的理由,要是我,就用正则表达式,或者直接保留后面两个部分
domain=url.split('.')
if domain[-1] in suffixs:
return string.join(domain[-2:],'.')
else:
return None #not valid domain