Publicsuffixlist is a Python library that implements a parser for the Public Suffix List (PSL). It allows users to extract the public suffix and registrable domain from a given hostname, supporting IDN (unicode and punycoded) and compliant with PSL test data. The library is pure Python, has no external dependencies, and ships with a bundled, frequently updated copy of the PSL. Its current version is 1.0.2.20260411, with new releases often tied to updates of the Public Suffix List itself.
pip install publicsuffixlistVerified import paths — ran on the pinned version, not inferred.
Initialise the PublicSuffixList object to use the bundled PSL file. Then, use `publicsuffix()` to get the public suffix and `privatesuffix()` to get the registrable domain. The library inherently handles Unicode and Punycode IDNs. Users can also provide their own PSL data.
Validate domain names using a dedicated domain validation library or custom logic before passing them to `publicsuffixlist` methods.
For UTF-8 domains, consider `psl = PublicSuffixList(idna_encoding=False)` if default behavior yields incorrect results. For ambiguous Punycode, explicitly `domain.encode("idna").decode("ascii")` before passing.Keep the `publicsuffixlist` library updated to get the latest bundled PSL. For critical applications, consider using the provided updater script (`python -m publicsuffixlist.update`) or fetching the latest list dynamically, though this library often updates its bundled list with releases.
Replace `publicsuffix` imports and usage with `publicsuffixlist`. Be aware of minor API differences if directly porting, though `publicsuffixlist` aims for similar functionality.
Install the package using pip: `pip install publicsuffixlist`
Ensure the input passed to the `publicsuffix` or `privatesuffix` methods is always a string or bytes-like object: `psl.publicsuffix('example.com')`Always check if the result of `publicsuffix()` or `privatesuffix()` is `None` before attempting further operations: `result = psl.publicsuffix('com'); if result: print(result.lower())`Import the `PublicSuffixList` class directly using `from publicsuffixlist import PublicSuffixList` and then instantiate it as `psl = PublicSuffixList()`