Python tldextract 模块 - 功能说明

tldextract准确地从URL的域名和子域名分离通用顶级域名或国家顶级域名。 例如,http://www.google.com,你只想取出连接的 'google' 部分。 每个人都会想到用 ‘.’ 拆分,来获取域名和后缀,但这是不准确的。并且只有当你想到简单的,例如.com域名,以 ‘.’ 截取最后2个元素得到结果。 想想如果解析,例如:http://forums.bbc.co.uk,上面天真的分裂方法是有问题的,你会得到 'co' 作为域名和“uk”为顶级域名,而不是“bbc”和“co.uk” 。tldextract有一个公共后缀列表 ,它可以匹配所有域名。 因此,给定一个URL,它从其域中知道其子域名,并且从其国家中知道其域名。

>>> import tldextract

>>> tldextract.extract('http://forums.news.cnn.com/')
ExtractResult(subdomain='forums.news', domain='cnn', suffix='com') >>> tldextract.extract('http://forums.bbc.co.uk/') # United Kingdom
ExtractResult(subdomain='forums', domain='bbc', suffix='co.uk') >>> tldextract.extract('http://www.worldbank.org.kg/') # Kyrgyzstan
ExtractResult(subdomain='www', domain='worldbank', suffix='org.kg')

ExtractResult是namedtuple,所以它以简单方法得到你想要的部分。

>>> ext = tldextract.extract('http://forums.bbc.co.uk')
>>> (ext.subdomain, ext.domain, ext.suffix)
('forums', 'bbc', 'co.uk')
>>> # rejoin subdomain and domain
>>> '.'.join(ext[:2])
'forums.bbc'
>>> # a common alias
>>> ext.registered_domain
'bbc.co.uk'

子域和后缀是可选的。 不是所有类似URL的输入都有一个子域或有效的后缀。

>>> tldextract.extract('google.com')
ExtractResult(subdomain='', domain='google', suffix='com') >>> tldextract.extract('google.notavalidsuffix')
ExtractResult(subdomain='google', domain='notavalidsuffix', suffix='') >>> tldextract.extract('http://127.0.0.1:8080/deployed/')
ExtractResult(subdomain='', domain='127.0.0.1', suffix='')

如果要重新加入整个命名的元组,无论是否找到子域或后缀:

>>> ext = tldextract.extract('http://127.0.0.1:8080/deployed/')
>>> # this has unwanted dots
>>> '.'.join(ext)
'.127.0.0.1.'
>>> # join each part only if it's truthy
>>> '.'.join(part for part in ext if part)
'127.0.0.1'

该模块通过实现从选择stackoverflow答案开始,从一个URL获取“域名”这个计算问题 。 然而,建议的正则表达式解决方案不涉及其它许多国家,如 com.au,如注册域parliament.uk。 公共后缀列表,这个模块也是如此。

安装 tldextract

最新发布的 PyPI:

pip install tldextract

或者最新的开发版本:

pip install -e 'git://github.com/john-kurkowski/tldextract.git#egg=tldextract'

命令行用法,按空格分开网址:

tldextract http://forums.bbc.co.uk
# forums bbc co.uk

注意缓存更新

当第一次运行该模块时,它会用实时HTTP请求更新其后缀列表。 这个更新的后缀集在无限期缓存/path/to/tldextract/.tld_set 。 (可以说运行时引导类似这样不应该是默认行为,就像生产系统,但我想要你有最新的后缀,特别是当我没有保持这个代码的最新)。 要避免此提取或控制缓存的位置,请通过设置后缀EXTRACT_CACHE环境变量或通过在后缀Extract初始化中设置cache_file路径来使用您自己的提取调用。

# extract callable that falls back to the included TLD snapshot, no live HTTP fetching
no_fetch_extract = tldextract.TLDExtract(suffix_list_urls=None)
no_fetch_extract('http://www.google.com') # extract callable that reads/writes the updated TLD set to a different path
custom_cache_extract = tldextract.TLDExtract(cache_file='/path/to/your/cache/file')
custom_cache_extract('http://www.google.com') # extract callable that doesn't use caching
no_cache_extract = tldextract.TLDExtract(cache_file=False)
no_cache_extract('http://www.google.com')

如果你想保持最新后缀定义 - 虽然他们不经常更改 - 偶尔删除缓存文件,运行更新命令

tldextract --update

或:

env TLDEXTRACT_CACHE="~/tldextract.cache" tldextract --update

也建议在升级此lib之后删除文件。

高级用法

为后缀列表数据指定自己的URL或文件

您可以指定自己的输入数据代替默认的Mozilla公共后缀列表:

extract = tldextract.TLDExtract(
suffix_list_urls=["http://foo.bar.baz"],
# Recommended: Specify your own cache file, to minimize ambiguities about where
# tldextract is getting its data, or cached data, from.
cache_file='/path/to/your/cache/file')

以上片段将与您指定的网址提取,在首先需要下载后缀列表(即如果cache_file不存在)。 如果你想从你的本地文件系统使用的输入数据,只需要使用file://协议:

extract = tldextract.TLDExtract(
suffix_list_urls=["file://absolute/path/to/your/local/suffix/list/file"],
cache_file='/path/to/your/cache/file')

请使用绝对路径suffix_list_urls关键字参数。 os.path是友好路径。

如果我传递一个无效的URL,我仍然得到一个结果,没有错误。 为什么会得到?

为了保持tldextract光控制线和开销,因为有大量的URL验证器在那里,这个库是非常宽松的输入。 如果有效的URL是对你很重要,调用之前先验证这些tldextract 。 这种宽松的态度降低了使用库的学习曲线,代价是使用户对URL的细微差别。 谁知道多少。 但在将来,我会考虑一次大修。例如,用户可以选择验证,接收结果中的异常或错误元数据。 tldextract GitHub 地址:https://github.com/john-kurkowski/tldextract

Python tldextract模块准确获取域名和后缀的更多相关文章

  1. python xlrd 模块(获取Excel表中数据)

    python xlrd 模块(获取Excel表中数据) 一.安装xlrd模块   到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了pyt ...

  2. Python tldextract模块

    最新发布的 PyPI: pip install tldextract 或者最新的开发版本: pip install -e 'git://github.com/john-kurkowski/tldext ...

  3. Python random模块(获取随机数)常用方法和使用例子

    random.randomrandom.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.uniformrandom.uniform(a, b),用 ...

  4. Python random模块(获取随机数)

    1.random.random 随机生成一个0到1的随机浮点数: 0 <= n < 1.0 In [2]: print random.random() 0.544824016934 2.r ...

  5. python random模块(获取随机数)的常用方法及示例

    random.randomrandom.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.uniformrandom.uniform(a, b),用 ...

  6. python datetime模块来获取当前的日期和时间

    #!/usr/bin/python # -*- coding: UTF- -*- import datetime i = datetime.datetime.now() print ("当前 ...

  7. Learning-Python【19】:Python常用模块(2)—— os、sys、shutil

    os模块:与操作系统相关的模块 import os # 获取当前的工作目录 print(os.getcwd()) # 切换工作目录 os.chdir(r'E:\Python\test') print( ...

  8. Python random模块 例子

    最近用到随机数,就查询资料总结了一下Python random模块(获取随机数)常用方法和使用例子. 1.random.random  random.random()用于生成一个0到1的随机符点数: ...

  9. Pythonrandom模块(获取随机数)常用方法和使用例子

    Python random模块(获取随机数)常用方法和使用例子 这篇文章主要介绍了Python random模块(获取随机数)常用方法和使用例子,需要的朋友可以参考下 random.random ra ...

随机推荐

  1. Spring boot 集成 Dubbo 快速搭建

    架构: 1.ZooKeeper:服务注册中心 2.api工程:提供对外暴露的服务API 3.provider:服务提供者 4.consumer:服务消费者 示例如下: (一)新建 Maven 项目 a ...

  2. 面向对象OONo.3单元总结

    一,JML语言 1)JML理论基础:JML是一类语言,用来描述一个方法或一个类的功能.以及这个类在实现这个功能时需要的条件.可能改变的全局变量.以及由于条件问题不能实现功能时这个方法或类的行为,具有明 ...

  3. 抓取oracle建表语句及获取建表ddl语句

    抓取oracle建表语句及获取建表ddl语句 1.抓取代码如下: 1.1.产生表的语法资料 DECLARE-- v_notPartTable VARCHAR2(1000):= '&2'; -- ...

  4. 20180904 定时器setTimeout和setInterval回调问题

    引用: setTimeout和setInterval两者的区别 setTimeout和setInterval的优缺点 setTimeout和setInterval详解 两者的作用都是在定时多少毫秒后回 ...

  5. constraint the design

  6. python之路-双下方法

    双下方法 定义: 双下方法是特殊方法,他是解释器提供的,由双下线加方法名加双下划线 __方法名__具有特殊意义的方法 双下方法主要是Python源码程序员使用的,元编程 我们在开发中尽量不要使用双下方 ...

  7. writing a usb driver(在国外的网站上复制下来的)

    Writing a Simple USB Driver   From Issue #120April 2004 Apr 01, 2004  By Greg Kroah-Hartman  in Soft ...

  8. http请求原理

    客户端发送一个HTTP请求到服务器的请求消息包括以下格式:请求行(request line).请求头部(header).空行和请求数据四个部分组成,下图给出了请求报文的一般格式. 请求行 HTTP响应 ...

  9. Leetcode 81. 搜索旋转排序数组 II

    题目链接 https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/description/ 题目描述 假设按照升序排序的数 ...

  10. Java装饰者模式(Decorator)

    一.定义 装饰模式的设计理念主要是以对客户端透明的方式动态扩展对象的功能,是继承关系的一个替代(继承会产生大量的子类,而且代码有冗余).装饰模式可以在不创造更多子类的情况下,将对象的功能加以扩展.装饰 ...