Skip to content
Please note that GitHub no longer supports your web browser.

We recommend upgrading to the latest Google Chrome or Firefox.

Learn more
Accurately separate the TLD from the registered domain and subdomains of a URL, using the Public Suffix List.
Branch: master
Clone or download
Latest commit 3732dfd Oct 16, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
tests Address pylint disable Oct 15, 2019
tldextract Address pylint disable Oct 15, 2019
tldextract_app fixes some code style errors Aug 1, 2016
.gitignore py.test, instead of hand-rolled test suite building and running Nov 16, 2015
.travis.yml Modernize testing Oct 15, 2019 2.2.2 Oct 16, 2019
LICENSE Update LICENSE from GitHub template Aug 19, 2019 Add LICENSE to MANIFEST Aug 30, 2016 Modernize testing Oct 15, 2019
pylintrc Disable some pylint checks Oct 15, 2019
pytest.ini pytest-pylint plugin Nov 16, 2015
setup.cfg Include LICENSE in wheel Oct 16, 2016 2.2.2 Oct 16, 2019
tox.ini Use the latest pylint that works in Python 2 Oct 15, 2019


Python Module PyPI version Build Status

tldextract accurately separates the gTLD or ccTLD (generic or country code top-level domain) from the registered domain and subdomains of a URL. For example, say you want just the 'google' part of ''.

Everybody gets this wrong. Splitting on the '.' and taking the last 2 elements goes a long way only if you're thinking of simple e.g. .com domains. Think parsing for example: the naive splitting method above will give you 'co' as the domain and 'uk' as the TLD, instead of 'bbc' and '' respectively.

tldextract on the other hand knows what all gTLDs and ccTLDs look like by looking up the currently living ones according to the Public Suffix List (PSL). So, given a URL, it knows its subdomain from its domain, and its domain from its country code.

>>> import tldextract

>>> tldextract.extract('')
ExtractResult(subdomain='', domain='cnn', suffix='com')

>>> tldextract.extract('') # United Kingdom
ExtractResult(subdomain='forums', domain='bbc', suffix='')

>>> tldextract.extract('') # Kyrgyzstan
ExtractResult(subdomain='www', domain='worldbank', suffix='')

ExtractResult is a namedtuple, so it's simple to access the parts you want.

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

Note subdomain and suffix are optional. Not all URL-like inputs have a subdomain or a valid suffix.

>>> tldextract.extract('')
ExtractResult(subdomain='', domain='google', suffix='com')

>>> tldextract.extract('google.notavalidsuffix')
ExtractResult(subdomain='google', domain='notavalidsuffix', suffix='')

>>> tldextract.extract('')
ExtractResult(subdomain='', domain='', suffix='')

If you want to rejoin the whole namedtuple, regardless of whether a subdomain or suffix were found:

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

By default, this package supports the public ICANN TLDs and their exceptions. You can optionally support the Public Suffix List's private domains as well.

This module started by implementing the chosen answer from this StackOverflow question on getting the "domain name" from a URL. However, the proposed regex solution doesn't address many country codes like, or the exceptions to country codes like the registered domain The Public Suffix List does, and so does this module.


Latest release on PyPI:

pip install tldextract

Or the latest dev version:

pip install -e 'git://'

Command-line usage, splits the url components by space:

# forums bbc

Note About Caching

Beware when first running the module, it updates its TLD list with a live HTTP request. This updated TLD set is cached indefinitely in /path/to/tldextract/.tld_set.

(Arguably runtime bootstrapping like that shouldn't be the default behavior, like for production systems. But I want you to have the latest TLDs, especially when I haven't kept this code up to date.)

To avoid this fetch or control the cache's location, use your own extract callable by setting TLDEXTRACT_CACHE environment variable or by setting the cache_file path in TLDExtract initialization.

# extract callable that falls back to the included TLD snapshot, no live HTTP fetching
no_fetch_extract = tldextract.TLDExtract(suffix_list_urls=None)

# 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')

# extract callable that doesn't use caching
no_cache_extract = tldextract.TLDExtract(cache_file=False)

If you want to stay fresh with the TLD definitions--though they don't change often--delete the cache file occasionally, or run

tldextract --update


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

It is also recommended to delete the file after upgrading this lib.

Advanced Usage

Public vs. Private Domains

The PSL maintains a concept of "private" domains.

PRIVATE domains are amendments submitted by the domain holder, as an expression of how they operate their domain security policy. … While some applications, such as browsers when considering cookie-setting, treat all entries the same, other applications may wish to treat ICANN domains and PRIVATE domains differently.

By default, tldextract treats public and private domains the same.

>>> extract = tldextract.TLDExtract()
>>> extract('')
ExtractResult(subdomain='waiterrant', domain='blogspot', suffix='com')

The following overrides this.

>>> extract = tldextract.TLDExtract(include_psl_private_domains=True)
>>> extract.update() # necessary until #66 is fixed
>>> extract('')
ExtractResult(subdomain='', domain='waiterrant', suffix='')

The thinking behind the default is, it's the more common case when people mentally parse a URL. It doesn't assume familiarity with the PSL nor that the PSL makes such a distinction. Note this may run counter to the default parsing behavior of other, PSL-based libraries.

Specifying your own URL or file for the Suffix List data

You can specify your own input data in place of the default Mozilla Public Suffix List:

extract = tldextract.TLDExtract(
    # Recommended: Specify your own cache file, to minimize ambiguities about where
    # tldextract is getting its data, or cached data, from.

The above snippet will fetch from the URL you specified, upon first need to download the suffix list (i.e. if the cache_file doesn't exist).

If you want to use input data from your local filesystem, just use the file:// protocol:

extract = tldextract.TLDExtract(

Use an absolute path when specifying the suffix_list_urls keyword argument. os.path is your friend.


Can you add suffix ____? Can you make an exception for domain ____?

This project doesn't contain an actual list of public suffixes. That comes from the Public Suffix List (PSL). Submit amendments there.

(In the meantime, you can tell tldextract about your exception by either forking the PSL and using your fork in the suffix_list_urls param, or adding your suffix piecemeal with the extra_suffixes param.)

If I pass an invalid URL, I still get a result, no error. What gives?

To keep tldextract light in LoC & overhead, and because there are plenty of URL validators out there, this library is very lenient with input. If valid URLs are important to you, validate them before calling tldextract.

This lenient stance lowers the learning curve of using the library, at the cost of desensitizing users to the nuances of URLs. Who knows how much. But in the future, I would consider an overhaul. For example, users could opt into validation, either receiving exceptions or error metadata on results.

Public API

I know it's just one method, but I've needed this functionality in a few projects and programming languages, so I've uploaded tldextract to App Engine. It's there on GAE's free pricing plan until Google cuts it off. Just hit it with your favorite HTTP client with the URL you want parsed like so:

curl ""
# {"domain": "bbc", "subdomain": "www", "suffix": ""}


Setting up

  1. git clone this repository.
  2. Change into the new directory.
  3. pip install tox

Alternatively you can install detox instead of tox to run tests in parallel.

Running the Test Suite

Run all tests against all supported Python versions:


Run all tests against a specific Python environment configuration:

tox -l
tox -e py37
You can’t perform that action at this time.