从给定的url中提取顶级域(tld)。
tld的Python项目详细描述
从给定的url中提取顶级域(tld)。TLD名称列表是 取自Mozilla。
可选地在非现有的TLD上引发异常或静默失败(如果 fail_silently参数设置为true)。
先决条件
- python 2.7、3.4、3.5、3.6、3.7和pypy
文档
文档可在Read the Docs上找到。
安装
pypi上的最新稳定版本:
pip install tld
或github的最新稳定版本:
pip install https://github.com/barseghyanartur/tld/archive/stable.tar.gz
或BitBucket的最新稳定版本:
pip install https://bitbucket.org/barseghyanartur/tld/get/stable.tar.gz
用法示例
除了下面的示例之外,请参见jupyter notebook 工作簿文件。
从给定的url获取tld名称作为字符串fromtldimportget_tldget_tld("http://www.google.co.uk")# 'co.uk'get_tld("http://www.google.idontexist",fail_silently=True)# None
获取tld作为an对象
fromtldimportget_tldres=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名称,忽略缺少的协议
fromtldimportget_tld,get_fldget_tld("www.google.co.uk",fix_protocol=True)# 'co.uk'get_fld("www.google.co.uk",fix_protocol=True)# 'google.co.uk'
将tld部件作为元组返回
fromtldimportparse_tldparse_tld('http://www.google.com')# 'com', 'google', 'www'
从给定的url获取第一级域名as stringfromtldimportget_fldget_fld("http://www.google.co.uk")# 'google.co.uk'get_fld("http://www.google.idontexist",fail_silently=True)# None
检查某些TLD是否为有效的TLD
fromtldimportis_tldis_tld('co.uk)# Trueis_tld('uk')# Trueis_tld('tld.doesnotexist')# Falseis_tld('www.google.com')# False
更新tld名称列表
要使用最新版本更新/同步tld名称,请运行以下命令 从您的终端:
update-tld-names
或者直接做:
fromtld.utilsimportupdate_tld_namesupdate_tld_names()
测试
只需键入:
./runtests.py
或使用毒物:
tox
或使用毒性检查特定环境:
tox -e py36
编写文档
保持以下层次结构。
===== 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/gpl 2.0/lgpl 2.1
支架
如有任何问题,请通过Author部分中提供的电子邮件与我联系。