提取链接域名
python的标准模块
urlib
提供解决方案 (python2中是urlparse,在python3中都整合到urllib中了)
from urllib.parse import urlparse
def parse_host():
url = "http://www.gov.cn/xinwen/2020-03/10/content_5489651.htm"
data = urlparse(url)
print(data)
domain = urlparse(url).netloc
print(domain)
if __name__ == '__main__':
parse_host()
输出
ParseResult(scheme='http', netloc='www.gov.cn', path='/xinwen/2020-03/10/content_5489651.htm', params='', query='', fragment='')
www.gov.cn