tldextract accurately separates a URL's subdomain, domain, and public suffix, using the Public Suffix List (PSL). It handles edge cases often missed by naive parsing methods. By default, it supports public ICANN TLDs and their exceptions, with optional support for private domains. The current version is 5.3.1, and the library maintains an active development and release cadence.
pip install tldextractVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic URL component extraction using `tldextract.extract()` and accessing the results as attributes of the `ExtractResult` object.
Access fields by attribute name (e.g., `result.subdomain`, `result.domain`, `result.suffix`) instead of indexing or unpacking.
If unpacking, adjust to expect four fields, or preferably, access fields by attribute name to avoid issues with future field additions.
Use the `top_domain_under_public_suffix` property instead, which has the same behavior but a more accurate name.
Upgrade your Python environment to 3.10 or later to continue using the latest `tldextract` versions.
To control caching, specify `cache_dir` when initializing `tldextract.TLDExtract()` or manage the `TLDEXTRACT_CACHE` environment variable. You can also explicitly trigger an update via `tldextract --update` CLI command.
If strict URL validation is required, pre-process the input string with a dedicated URL validation library (e.g., `urllib.parse.urlsplit`) before passing it to `tldextract`.
pip install tldextract
Ensure your system's CA certificates are up-to-date, configure the REQUESTS_CA_BUNDLE environment variable, or for development purposes, disable SSL verification for PSL updates (e.g., `tldextract.update_public_suffix_list(requests_session=requests.Session(), extra_kwargs={'verify': False})`).Set a custom writable cache directory using `tldextract.set_cache_dir('/path/to/a/writable/directory')` before calling `tldextract.extract()`, or adjust the permissions for the default cache location.