publicsuffix2: Public Suffix List Parser

2.20191221 · maintenance · verified Thu Apr 09

publicsuffix2 is a Python library designed to parse and identify the public suffix of a domain name using the official Public Suffix List (PSL). It is a fork of the original 'publicsuffix' package, aiming for API compatibility. The current version is 2.20191221, released in late 2019, meaning its bundled PSL data is not recent.

Warnings

Install

Imports

Quickstart

Initialize PublicSuffixList and use `get_public_suffix()` to extract the registrable domain from a given hostname. It returns None for hosts that are not valid domains or do not have a recognizable public suffix.

from publicsuffix2 import PublicSuffixList

# Initialize the PublicSuffixList parser
psl = PublicSuffixList()

# Get the public suffix for various domains
print(f"google.com -> {psl.get_public_suffix('www.google.com')}")
print(f"co.uk -> {psl.get_public_suffix('test.co.uk')}")
print(f"github.io -> {psl.get_public_suffix('myblog.github.io')}")
print(f"local domain (should be None) -> {psl.get_public_suffix('localhost')}")
print(f"invalid domain (should be None) -> {psl.get_public_suffix('not-a-domain')}")

view raw JSON →