tldextract

Accurately splits any URL or hostname into subdomain, domain, and public suffix using the Public Suffix List.

Library
PyPI
v5.3.2
2,024 stars
BSD 3-Clause License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
80 /100 Excellent
Development Activity 80
Maintenance 60
Community 80
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
80 /100 Excellent
Architecture 88
Code Quality 92
Innovation 75
Learning Curve 65

tldextract solves a problem that naive string splitting gets wrong: given a hostname like forums.bbc.co.uk, which part is the “domain”? Splitting on the last dot gives co, but the real registrable domain is bbc.co.uk. tldextract answers this correctly by consulting the community-maintained Public Suffix List (PSL), which enumerates both public suffixes like .com and .co.uk and “private” suffixes like blogspot.com that browsers also treat specially for cookie scoping and security decisions.

The library fetches the PSL over HTTP on first use, caches it to disk (XDG-compliant, keyed per Python environment to avoid cross-venv corruption), and falls back to a bundled snapshot if no network or cache is available. Extraction itself is a reverse-label trie walk that correctly handles PSL wildcard rules, exceptions, and IDNA/punycode-encoded labels. Results come back as an ExtractResult dataclass exposing not just subdomain/domain/suffix but derived properties like fqdn, ipv4/ipv6 detection, and reverse-DNS notation — plus a tldextract CLI for one-off lookups.

What You Get

  • A single tldextract.extract() function and TLDExtract class returning an ExtractResult with subdomain, domain, suffix, and private-domain metadata
  • Automatic Public Suffix List fetching, disk caching, and offline snapshot fallback so extraction works without network access
  • Punycode/IDNA-aware suffix matching so internationalized domains resolve to the correct suffix boundary
  • A tldextract command-line tool for quick hostname lookups, with JSON output support
  • Derived ExtractResult properties (fqdn, ipv4, ipv6, reverse_domain_name, top_domain_under_public_suffix) that cover common follow-on needs without extra code

Common Use Cases

  • Grouping web analytics or log data by registrable domain instead of raw hostname
  • Normalizing or validating user-submitted domains/emails before storing them
  • Building cookie-scoping or same-site security checks that need the real registrable domain, not just the last two labels
  • Extracting root domains from crawled URLs for deduplication in a web scraper or crawler
  • Reverse-DNS-style namespacing for plugin/package identifiers derived from a domain

Under The Hood

Architecture The library separates concerns cleanly across four small modules: remote.py handles lenient netloc extraction and IPv4/IPv6 detection without raising on malformed input; cache.py implements a generic, file-locked disk cache keyed by a hash of its inputs, used both for HTTP response caching and for memoizing the parsed suffix list; suffix_list.py orchestrates fetching the PSL from a list of mirror URLs (falling back to a bundled .tld_set_snapshot file), parsing it into public/private suffix lists via regex, and threading that through the cache layer; and tldextract.py ties it together in the TLDExtract class, building a reverse-label Trie from the suffix lists and walking it label-by-label from the end of the hostname to find the longest matching suffix, correctly handling PSL wildcard (*) and exception (!label) rules. A thin cli.py argparse frontend exposes the same functionality as a command-line tool. Nothing here would break under normal use if the core Trie/suffix_index logic changed, since it’s isolated behind the TLDExtract public API.

Tech Stack Pure Python 3.10+, targeting CPython 3.10-3.14 and PyPy 3.10/3.11. Runtime dependencies are minimal and purposeful: idna for punycode decoding, requests plus requests-file for fetching suffix lists (including from local file:// URLs), and filelock for safe concurrent disk-cache access. Packaging uses setuptools with setuptools-scm for git-tag-derived versioning (no manually maintained version string). Development tooling runs through uv and tox/tox-uv for a matrix of interpreter versions.

Code Quality The test suite is extensive and multi-layered: unit tests across main_test.py, cli_test.py, custom_suffix_test.py, test_cache.py, test_parallel.py, and test_trie.py; runnable doctests embedded directly in docstrings and exercised via sybil and pytest --doctest-modules; and snapshot testing via syrupy for suffix-list parsing output. HTTP behavior is tested with the responses mocking library rather than hitting the network. mypy --strict is enforced project-wide, and ruff lint covers a broad rule set (bugbear, comprehensions, docstrings, naming, pyupgrade) in addition to formatting. CI runs the full matrix (three OSes × seven Python/PyPy versions) plus separate lint, typecheck, and codestyle jobs on every push and PR.

API Design The public surface is intentionally small — one function (extract), one class (TLDExtract), one result dataclass — but the result dataclass carries well-named derived properties (fqdn, ipv4, ipv6, reverse_domain_name, top_domain_under_public_suffix, top_domain_under_registry_suffix) that answer the follow-up questions callers usually have to write themselves. The distinction between suffix (affected by include_psl_private_domains) and registry_suffix (always the registrar-level suffix) is a genuinely useful, if subtle, piece of API design for callers who need to reason about private PSL domains like blogspot.com. The library documents, rather than silently papering over, the boundary between suffix-boundary detection and hostname validation — it explicitly does not canonicalize or validate hostnames, and the README calls out the security implications of that choice for equality checks and allowlists.

Used by 6 apps in this directory

Vue
52%
AGPL 3.0

Frappe CRM

CRM

3,613

Open-source CRM with unlimited users, built-in Twilio, Exotel, WhatsApp, and ERPNext integrations — self-host in minutes.

View details
92
Repo Health
68
Technical
77
Dependency
Built with
Vue 52%
Python 34%
JavaScript 11%
Updated 4 days ago
TypeScript
39%
Apache 2.0

Label Studio

AI Development · Data Engineering

28,358

Label Studio is an open-source, multi-type data labeling platform that lets teams annotate images, text, audio, video, and time series data with a configurable XML-based UI and export annotations in formats ready for any ML framework.

View details
93
Repo Health
87
Technical
67
Dependency
Built with
TypeScript 39%
JavaScript 27%
Python 25%
Updated 4 days ago
Python
55%
Other

PostHog

Ab Testing Experimentation · Analytics · Developer Tools

39,975

The all-in-one open source product platform combining analytics, session replay, feature flags, error tracking, AI observability, and a built-in data warehouse in a single self-hostable stack.

View details
92
Repo Health
80
Technical
65
Dependency
Built with
Python 55%
TypeScript 36%
Updated 4 days ago
Python
57%
Other

Sentry

Analytics · Developer Tools · Monitoring

44,862

Developer-first error tracking and performance monitoring platform with AI-powered root-cause analysis across 20+ languages and frameworks.

View details
95
Repo Health
80
Technical
69
Dependency
Built with
Python 57%
TypeScript 41%
Updated 4 days ago
Python
78%
AGPL 3.0

Skyvern

AI Agents · Automation

23,088

Skyvern (YC S2023) automates browser-based workflows by pairing LLMs with computer vision, letting agents click, fill, and extract data on sites they've never seen, without brittle XPath selectors that break on every layout change.

View details
89
Repo Health
82
Technical
70
Dependency
Built with
Python 78%
TypeScript 20%
Updated 4 days ago
Python
94%
Apache 2.0

SWIRL

Data Engineering · Databases · Search

3,047

Federated AI search and RAG across 100+ enterprise sources—no data extraction, no vector database required.

View details
62
Repo Health
83
Technical
65
Dependency
Built with
Python 94%
Updated 6 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers