Skip to main content

TLDParse

Split a given domain into its different parts (subdomain, domain, suffix).

It was built in the aim to be very fast. To do so, it starts in reverse, going from the TLD down to any subdomains.

Installation

pip install tldparse

Basic usage

The following code will show you the basic usage you can have from this library.

from tldparse import DomainResult

parsed = DomainResult('https://example.github.io/with/some/path')
print(parsed.tld) # io
print(parsed.domain) # github
print(parsed.subdomain) # example

Common usage

A common usage is to check whereas the given domain is the fqdn or if it has subdomains

from tldparse import DomainResult

email = 'user@sub.domain.com'
mbox, domain = email.split('@')

parsed = DomainResult(domain)
assert parsed.fqdn == domain, "Please register using a domain with no subdomain parts"

Advanced usage

Replacing TLDParse with a custom one:

DomainResult class use the default TLDParse object that relies on the list of suffixes provided by Mozilla. (The list is freely available at https://publicsuffix.org/list/public_suffix_list.dat)

But you can either pass another parser, such as:

class MyCustomParser:
    def __call__(self, domain, private=True):
        # PLEASE DON'T DO THAT
        return domain.rsplit('.', 2)  # Consider a TLD has only one part. Ignores things such as ".co.uk"

from tldparse import DomainResult

parser = MyCostumParser()

result = DomainResult('example.github.com', parser=parser)
print(result.fqdn)  # github.com

result = DomainResult('bbc.co.uk', parser=parser)
print(result.fqdn)  # co.uk  # Too bad

Using private subdomains:

By default, the library will consider the subdomains as part of the fqdn for certain domain. But you can change this behavior by setting the private parameter to True.

from tldparse import DomainResult

result = DomainResult('example.github.io')
print(result.fqdn) # example.github.io

result = DomainResult('example.github.io', private=False)
print(result.fqdn) # github.io

Adding/Removing TLDs entries:

You can add/remove entries to the parser by using the add/remove methods of the tldparse object:

from .tldparse import TLDParse, DomainResult

# Adding a custom domain
parser.add('weebly.co.uk')  # added in private
result = DomainResult('example.weebly.co.uk', parser=parser, private=False)
print(result.suffix)  # 'co.uk'
print(result.domain)  # 'weebly'
print(result.subdomain)  # 'example'

result = DomainResult('example.weebly.co.uk', parser=parser)
print(result.suffix)  # 'weebly.co.uk'
print(result.domain)  # 'example'

# Removing a domain
TLDParse.remove('github.io')
result = DomainResult('example.github.io')
print(result.suffix)  # 'io'
print(result.domain)  # 'github'
print(result.subdomain)  # 'example'

Support

If you find an issue, please open a support requests, or even better, a Pull Requests :)

Thank you!

Release files for tldparse 1.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tldparse 1.0.5
File Size Uploaded
tldparse-1.0.5.tar.gz 93.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tldparse 1.0.5
File Interpreter ABI Platform
tldparse-1.0.5-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 185.4 kB

Release files / tldparse-1.0.5.tar.gz

Download URL tldparse-1.0.5.tar.gz
Size 93.5 kB
Tags Source
SHA-256 checksum
How to use checksums
47ed932c0937fec8545ad2b7d89429eea941b9e3301223c363db2d3f8f343412
BLAKE2b-256 checksum
How to use checksums
bbd323776af21d592a1de87fc3b78dc7b79a74180e91888112f6fd9234f03797
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.6

Release files / tldparse-1.0.5-py2.py3-none-any.whl

Download URL tldparse-1.0.5-py2.py3-none-any.whl
Size 91.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
4758810bf461246807f89da92a90c4120cd3675ee423c2431a0a5cfacd52d9eb
BLAKE2b-256 checksum
How to use checksums
fce243b77ccbf36814e65a8111d121ab3831cf1fba33b0ca4564c47b1af8c4db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.6

Release history Release notifications | RSS feed

This release

1.0.5 This release

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page