从给定的URL中提取顶级域名(TLD)。
项目描述
从给定的URL中提取顶级域名(TLD)。TLD名称列表来自公共后缀。
可选地,在非存在的TLD上抛出异常或在设置fail_silently参数为True时静默失败。
先决条件
Python 3.7, 3.8, 3.9, 3.10或3.11。
文档
文档可在Read the Docs上找到。
安装
PyPI上的最新稳定版本
pip install tld
或GitHub上的最新稳定版本
pip install https://github.com/barseghyanartur/tld/archive/stable.tar.gz
使用示例
除了下面的示例外,请参阅jupyter notebook工作簿文件。
从给定的URL中获取TLD名称 作为字符串
from tld import get_tld
get_tld("http://www.google.co.uk")
# 'co.uk'
get_tld("http://www.google.idontexist", fail_silently=True)
# None
获取TLD 作为对象
from tld import get_tld
res = get_tld("http://some.subdomain.google.co.uk", as_object=True)
res
# 'co.uk'
res.subdomain
# 'some.subdomain'
res.domain
# 'google'
res.tld
# 'co.uk'
res.fld
# 'google.co.uk'
res.parsed_url
# SplitResult(
# scheme='http',
# netloc='some.subdomain.google.co.uk',
# path='',
# query='',
# fragment=''
# )
获取TLD名称,忽略缺失的协议
from tld import get_tld, get_fld
get_tld("www.google.co.uk", fix_protocol=True)
# 'co.uk'
get_fld("www.google.co.uk", fix_protocol=True)
# 'google.co.uk'
返回TLD部分作为元组
from tld import parse_tld
parse_tld('http://www.google.com')
# 'com', 'google', 'www'
从给定的URL中获取一级域名作为字符串
from tld import get_fld
get_fld("http://www.google.co.uk")
# 'google.co.uk'
get_fld("http://www.google.idontexist", fail_silently=True)
# None
检查某个顶级域名是否为有效顶级域名
from tld import is_tld
is_tld('co.uk)
# True
is_tld('uk')
# True
is_tld('tld.doesnotexist')
# False
is_tld('www.google.com')
# False
更新顶级域名名称列表
要更新/同步顶级域名名称与最新版本,请在终端运行以下命令
update-tld-names
或者简单地进行以下操作
from tld.utils import update_tld_names
update_tld_names()
注意,这将更新所有注册的顶级域名源解析器(而不仅仅是Mozilla获取的顶级域名名称列表)。为了运行单个解析器的更新,将那个解析器的uid作为参数追加。
update-tld-names mozilla
自定义顶级域名解析器
默认情况下,顶级域名名称列表从Mozilla获取。解析实现在tld.utils.MozillaTLDSourceParser类中。如果您想使用另一个解析器,可以继承tld.base.BaseTLDSourceParser,提供uid、source_url、local_path并实现get_tld_names方法。以tld.utils.MozillaTLDSourceParser作为此类实现的良好示例。然后您可以使用get_tld(以及其他tld模块函数),如下所示
from tld import get_tld
from some.module import CustomTLDSourceParser
get_tld(
"http://www.google.co.uk",
parser_class=CustomTLDSourceParser
)
自定义顶级域名名称列表
您可以维护自己的顶级域名名称列表版本(甚至多个),同时与内置的顶级域名名称列表一起使用。
然后您可以将它们存储在本地,并提供如下路径
from tld import get_tld
from tld.utils import BaseMozillaTLDSourceParser
class CustomBaseMozillaTLDSourceParser(BaseMozillaTLDSourceParser):
uid: str = 'custom_mozilla'
local_path: str = 'tests/res/effective_tld_names_custom.dat.txt'
get_tld(
"http://www.foreverchild",
parser_class=CustomBaseMozillaTLDSourceParser
)
# 'foreverchild'
同样适用于一级域名
from tld import get_fld
get_fld(
"http://www.foreverchild",
parser_class=CustomBaseMozillaTLDSourceParser
)
# 'www.foreverchild'
注意,在上述两个示例中,原始顶级域名名称文件已按以下方式进行修改
...
// ===BEGIN ICANN DOMAINS===
// This one actually does not exist, added for testing purposes
foreverchild
...
释放资源
为了释放由自定义顶级域名名称加载所占用的内存,使用带有tld_names_local_path参数的reset_tld_names函数。
from tld import get_tld, reset_tld_names
# Get TLD from a custom TLD names parser
get_tld(
"http://www.foreverchild",
parser_class=CustomBaseMozillaTLDSourceParser
)
# Free resources occupied by the custom TLD names list
reset_tld_names("tests/res/effective_tld_names_custom.dat.txt")
故障排除
如果在此处列出的域名名称无法识别,请确保您虚拟环境中具有最新的顶级域名名称版本
update-tld-names
要为单个解析器更新顶级域名名称列表,请指定它作为参数
update-tld-names mozilla
测试
只需输入
pytest
或者使用tox
tox
或者使用tox来检查特定环境
tox -e py39
编写文档
保持以下层次结构。
=====
title
=====
header
======
sub-header
----------
sub-sub-header
~~~~~~~~~~~~~~
sub-sub-sub-header
^^^^^^^^^^^^^^^^^^
sub-sub-sub-sub-header
++++++++++++++++++++++
sub-sub-sub-sub-sub-header
**************************
许可证
MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later
支持
有关安全问题,请联系作者部分提供的电子邮件。
有关总体问题,请访问GitHub。
项目详情
下载文件
下载适合您平台的文件。如果您不确定该选择哪一个,请了解更多关于安装包的信息。
源代码分发
构建分发
tld-0.13.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 93dde5e1c04bdf1844976eae440706379d21f4ab235b73c05d7483e074fb5629 |
|
MD5 | 6e43346e0188919bdd9eca2653d9c56b |
|
BLAKE2b-256 | 192b678082222bc1d2823ea8384c6806085b85226ff73885c703fe0c7143ef64 |
tld-0.13-py2.py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | f75b2be080f767ed17c2338a339eaa4fab5792586319ca819119da252f9f3749 |
|
MD5 | 1d916491723cd1682c1a9cc0d5ebb53c |
|
BLAKE2b-256 | ea75779ddeaf4d847ba0021ad99d1b615a853f2a5762bd5d118273c7f7673c38 |