python 提取链接中的域名

提取链接域名

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

猜你喜欢

转载自www.cnblogs.com/ppwang06/p/12469157.html