TLD Extraction Library

0.13.2 · active · verified Thu Apr 09

The `tld` library provides functions to reliably extract the top-level domain (TLD) from a given URL, along with subdomains and full domain names. It leverages the Public Suffix List to ensure accuracy. The current version is 0.13.2, and it typically sees several updates per year, though major versions may have longer gaps.

Warnings

Install

Imports

Quickstart

This example demonstrates extracting the TLD from a URL, both as a string and as an object for more detailed information. It also highlights the optional `update_tld_names` call and how to handle invalid URLs using `fail_silently`.

from tld import get_tld
from tld.utils import update_tld_names

# It's good practice to update the TLD names regularly
# This fetches the latest Public Suffix List
# For production, consider running this in a scheduled job, not on every startup.
# update_tld_names()

url1 = "http://www.google.co.uk"
tld1 = get_tld(url1)
print(f"TLD for '{url1}': {tld1}")

url2 = "https://sub.domain.example.com/path?query=1"
obj2 = get_tld(url2, as_object=True)
print(f"TLD for '{url2}': {obj2.tld}")
print(f"Subdomain: {obj2.subdomain}")
print(f"Domain: {obj2.domain}")
print(f"Full domain: {obj2.fld}")

try:
    invalid_url = "ftp://invalid-url"
    get_tld(invalid_url, fail_silently=False)
except Exception as e:
    print(f"Error extracting TLD for '{invalid_url}': {e}")

view raw JSON →