自学内容网 自学内容网

Python 使用 tldextract 提取域名

'''
安装:
python -m pip install tldextract==5.1.2
第三方库地址:
https://pypi.org/project/tldextract/
'''
# -*- coding: UTF-8 -*-

from tldextract import tldextract

url = "https://www.baidu.com/"  # 1
url = 'https://blog.csdn.net/weixin_44285988/article/details/89235814'  # 2
url = 'http://221.219.102.5:54321/api/getweather?cityid=334'    # 3
url = 'http://221.219.102.5/api/getweather?cityid=334'  # 4

val = tldextract.extract(url)

print(val)
print(val.domain)
print(val.registered_domain)

'''
输出:
1
ExtractResult(subdomain='www', domain='baidu', suffix='com', is_private=False)
baidu
baidu.com

2
ExtractResult(subdomain='blog', domain='csdn', suffix='net', is_private=False)
csdn
csdn.net

3
ExtractResult(subdomain='', domain='221.219.102.5', suffix='', is_private=False)
221.219.102.5

4
ExtractResult(subdomain='', domain='221.219.102.5', suffix='', is_private=False)
221.219.102.5
'''
'''
参考:
Python url中提取域名(获取域名、获取顶级域名、域协议、域路径、tldextract、urlparse)
https://mohen.blog.csdn.net/article/details/107663143
'''

原文地址:https://blog.csdn.net/yudiandian2014/article/details/140617862

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!