tldextract: Accurately separates a URL's subdomain, domain, and public suffix, using the Public Suffix List (PSL). By default, this includes the public ICANN TLDs and their exceptions. You can optionally support the Public Suffix List's private domains as well. · Pythie
Accurately separates a URL's subdomain, domain, and public suffix, using the Public Suffix List (PSL). By default, this includes the public ICANN TLDs and their exceptions. You can optionally support the Public Suffix List's private domains as well.
Maintained.Last release 8 weeks ago, commits in 6 of the last 26 weeks. Weaker on contributor spread.
tldextract accurately separates a URL's subdomain, domain, and public suffix,
using the Public Suffix List (PSL).
Why? Naive URL parsing like splitting on dots fails for domains like
forums.bbc.co.uk (gives "co" instead of "bbc"). tldextract handles the edge
cases, so you don't have to.
Quick Start
>>> import tldextract
>>> tldextract.extract('http://forums.news.cnn.com/')
ExtractResult(subdomain='forums.news', domain='cnn', suffix='com', is_private=False)
>>> tldextract.extract('http://forums.bbc.co.uk/')
ExtractResult(subdomain='forums', domain='bbc', suffix='co.uk', is_private=False)
>>> # Access the parts you need
>>> ext = tldextract.extract('http://forums.bbc.co.uk')
>>> ext.domain
'bbc'
>>> ext.top_domain_under_public_suffix
'bbc.co.uk'
>>> ext.fqdn
'forums.bbc.co.uk'
Install
pip install tldextract
How-to Guides
How to disable HTTP suffix list fetching for production
Or set the default for new extractors with an empty environment variable. Set
it before importing tldextract to affect the module-level extract function:
export TLDEXTRACT_PUBLIC_SUFFIX_LIST_URLS=""
The cache key includes the configured URLs. An empty value does not reuse a
cache entry fetched with the standard URLs; if no entry exists for the empty
URL list, tldextract uses its bundled snapshot.
Unlike the formal PSL algorithm,
tldextract does not apply the implicit * rule when no suffix matches. For an
unlisted final label, suffix remains empty so callers can distinguish configured
public suffixes from unknown, invalid, or internal hostnames.
Hostname representation
tldextract identifies public suffix boundaries but does not validate or
canonicalize hostnames. Returned strings can retain the input's casing and its
Unicode or ASCII-compatible encoding (ACE, commonly called Punycode).
Equivalent IDNA hostnames can therefore produce textually different values even
when tldextract identifies the same suffix boundary.
Before using suffix, fqdn, or joined-domain properties in equality checks,
allowlists, blocklists, or other security decisions, validate and normalize
both hostnames to the same IDNA representation. IDNA defines label equivalence
in terms of A-labels. See
RFC 5890, section 2.3.2.4.
Caching behavior
By default, tldextract fetches the latest Public Suffix List on first use and
caches it indefinitely in $HOME/.cache/python-tldextract.
URL validation
tldextract accepts any string and is very lenient. It prioritizes ease of use
over strict validation, extracting domains from any string, even partial URLs or
non-URLs.
uv run pytest # Fast local suite
uv run tox -e py310 # Supported-version suite
uv run tox --parallel # Full matrix
uv run ruff format . # Format code
History
This package started from a
StackOverflow answer
about regex-based domain extraction. The regex approach fails for many domains,
so this library switched to the Public Suffix List for accuracy.
Alternatives
Use instead of tldextract: tools that do the same job, closest first.