0
点赞
收藏
分享

微信扫一扫

Python:tldextract库查询域名的顶级后缀


要获取域名的顶级后缀,不能简单的用字符串拆分获取

原理:根据公共后缀列表,识别是域名的顶级后缀

公共后缀列表:

  • https://publicsuffix.org/
  • https://github.com/publicsuffix/list
  • https://publicsuffix.org/list/public_suffix_list.dat

Python实现的库

  • https://github.com/john-kurkowski/tldextract
  • https://github.com/barseghyanartur/tld

安装

pip install tldextract

代码示例

# -*- coding: utf-8 -*-

import tldextract

result = tldextract.extract('www.xiaomi.com.cn')
print(result)
# ExtractResult(subdomain='www', domain='xiaomi', suffix='com.cn')

参考

  • Python tldextract模块准确获取域名和后缀


举报

相关推荐

0 条评论