Accurately separate the TLD from the registered domain and subdomains of a URL, using the Public Suffix List.
Pull request Compare This branch is 216 commits behind john-kurkowski:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Python Module

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. 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', tld='com')
>>> tldextract.extract('') # United Kingdom
ExtractResult(subdomain='forums', domain='bbc', tld='')
>>> tldextract.extract('') # Kyrgyzstan
ExtractResult(subdomain='www', domain='worldbank', tld='')

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

>>> ext = tldextract.extract('')
>>> ext.domain
>>> '.'.join(ext[:2]) # rejoin subdomain and domain

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:

$ python -m tldextract.tldextract
forums bbc

Run tests:

$ python -m tldextract.tests.all

Note About Caching & Advanced Usage

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:

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

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

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

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

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", "tld": ""}