publicsuffixlist implement
Maintained. Last release 2 days ago, commits in 3 of the last 26 weeks. Weaker on commit activity and contributor spread.
pip install publicsuffixlistpublicsuffixlist
Public Suffix List parser implementation for Python 3.8+.
- Compliant with TEST DATA.
- Supports IDN (unicode and punycoded).
- Supports Python3.8+.
- Shipped with built-in PSL and an updater script.
- Written in Pure Python with no library dependencies.
Install
publicsuffixlist can be installed via pip.
$ pip install publicsuffixlist
Usage
Basic Usage:
from publicsuffixlist import PublicSuffixList
psl = PublicSuffixList()
# Uses built-in PSL file
print(psl.publicsuffix("www.example.com")) # "com"
# The longest public suffix part
print(psl.privatesuffix("www.example.com")) # "example.com"
# The shortest domain assigned for a registrant
print(psl.privatesuffix("com")) # None
# Returns None if no private (non-public) part found
print(psl.publicsuffix("www.example.unknownnewtld")) # "unknownnewtld"
# New TLDs are valid public suffix by default
print(psl.publicsuffix("www.example.香港")) # "香港"
# Accepts unicode
print(psl.publicsuffix("www.example.xn--j6w193g")) # "xn--j6w193g"
# Accepts Punycode IDNs by default
print(psl.privatesuffix("WWW.EXAMPLE.COM")) # "example.com"
# Returns in lowercase by default
print(psl.privatesuffix("WWW.EXAMPLE.COM", keep_case=True)) # "EXAMPLE.COM"
# kwarg `keep_case=True` to disable the case conversion
The latest PSL is packaged once a day. If you need to parse your own version,
it can be passed as a file-like iterable object, or just a str:
with open("latest_psl.dat", "rb") as f:
psl = PublicSuffixList(f)