Skip to main content

tk-normalizer

Python PyPI License: MIT

URL normalization library for creating consistent URL representations.

Purpose

The URL normalization process creates a mechanism to provide equivalence between URLs with varying string, protocol, scheme, and query parameter ordering. This library helps create normalized representations of URLs for consistent storage, comparison, and analysis.

Installation

pip install tk-normalizer

Quick Start

from tk_normalizer import TkNormalizer

# Simple usage - str() returns just the normalized URL
normalized = TkNormalizer("http://www.Example.com/path?b=2&a=1&utm_source=test")
print(str(normalized))  # Output: example.com/path?a=1&b=2

# Get full details with dict()
print(dict(normalized))  # Returns all fields including query_string, path, and hashes

Features

URL Normalization

The following URLs all normalize to the same normalized form:

https://example.com/
http://www.example.com/
http://www.example.com
http://www.example.com/#my_search_engine_is_great
https://www.example.com/?utm_campaign=SomeGoogleCampaign
https://www.example.com/?utm_source=because&utm_campaign=SomeGoogleCampaign

All normalize to: example.com

Normalization Process

URLs are normalized through the following steps:

  • ✅ Protocol and www subdomains removed
  • ✅ Lowercased
  • ✅ Trailing slashes removed
  • ✅ Query parameters reordered alphabetically by key
  • ✅ Duplicate query parameter key/value pairs removed
  • ✅ Common tracking parameters removed (utm_*, gclid, fbclid, etc.)
  • ✅ Non-HTTP(S) protocols rejected
  • ✅ Localhost URLs rejected

Tracking Parameters Removed

The following tracking parameters are automatically removed during normalization:

  • utm_* (all utm parameters)
  • gclid, fbclid, dclid (click identifiers)
  • _ga, _gid, _fbp, _hjid (analytics cookies)
  • msclkid (Microsoft Ads)
  • aff_id, affid (affiliate tracking)
  • referrer, adgroupid, srsltid
  • gaa_* (all Google Extended Access parameters), eafs_enabled (its companion flag)

Advanced Usage

Getting Full Normalization Details

from tk_normalizer import TkNormalizer

normalizer = TkNormalizer("http://blog.example.com/page?b=2&a=1")

# Use str() for just the normalized URL
print(str(normalizer))  # blog.example.com/page?a=1&b=2

# Use dict() for complete normalization data
result = dict(normalizer)
print(result)
# {
#   'normalized_url': 'blog.example.com/page?a=1&b=2',
#   'friendly_url': 'http://blog.example.com/page?a=1&b=2',
#   'parent_normalized_url': 'blog.example.com',
#   'root_normalized_url': 'example.com',
#   'registrable_domain': 'example.com',
#   'query_string': 'a=1&b=2',
#   'path': '/page',
#   'normalized_url_hash': '...',
#   'parent_normalized_url_hash': '...',
#   'root_normalized_url_hash': '...',
#   'registrable_domain_hash': '...',
#   'friendly_url_hash': '...'
# }

Error Handling

from tk_normalizer import TkNormalizer, InvalidUrlException

try:
    normalizer = TkNormalizer("not a valid url")
except InvalidUrlException as e:
    print(f"Invalid URL: {e}")

Accessing Individual Components

from tk_normalizer import TkNormalizer

normalizer = TkNormalizer("https://blog.example.com/path?a=1")

# Dict-like access to individual fields
print(normalizer["normalized_url"])  # blog.example.com/path?a=1
print(normalizer["parent_normalized_url"])  # blog.example.com
print(normalizer["root_normalized_url"])  # example.com
print(normalizer["registrable_domain"])  # example.com
print(normalizer["query_string"])  # a=1
print(normalizer["path"])  # /path

# Iterate over available fields
for key in normalizer:
    print(f"{key}: {normalizer[key]}")

# Get all field names
print(normalizer.keys())

Which URL Form to Use

need use
comparing, joining, deduping, counting normalized_url (or normalized_url_hash)
showing a URL to a person friendly_url (original casing, tracking params removed)
linking out, fetching, redriving the original URL you passed in
grouping by site registrable_domain

normalized_url is lowercased on purpose, so case variants of the same page compare equal. Keep the original URL next to it wherever you need to reach the page again.

Hashing

For efficient storage and comparison, SHA-256 hashes are computed for:

  • The normalized URL
  • The parent normal URL (domain without path)
  • The root normal URL (last two labels of the host)
  • The registrable domain
  • The friendly URL

This provides fixed-length representations suitable for database indexing. Storing the 32-byte digest (bytes.fromhex(...)) instead of the 64-character hex string halves the key size.

Important Caveats

While this normalization process works well for most use cases, there are some limitations:

  1. www subdomain removal: Technically, www.example.com and example.com could serve different content, though this is rare in practice.

  2. Case sensitivity: URLs are lowercased, but some servers are case-sensitive for paths.

  3. Tracking parameters: New tracking parameters emerge over time and may not be in the removal list.

  4. Fragment removal: URL fragments (#anchors) are removed, which may affect single-page applications.

  5. Root vs registrable domain: root_normalized_url is the last two labels of the host, so it is wrong under multi-label public suffixes: news.bbc.co.uk gets the root co.uk. registrable_domain uses the Public Suffix List (ICANN section, bundled with tldextract) and gives bbc.co.uk. Group sites by registrable_domain. root_normalized_url is unchanged so existing consumers do not shift.

Development

Setting Up Development Environment

# Clone the repository
git clone https://github.com/terakeet/tk-normalizer.git
cd tk-normalizer

# Install development dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run tests with coverage
pytest --cov=tk_normalizer

# Run linting
ruff check src tests

Running Tests

# Run all tests
pytest

# Run with verbose output
pytest -v

# Run specific test file
pytest tests/test_normalizer.py

# Run with coverage report
pytest --cov=tk_normalizer --cov-report=html

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Deploying to PYPI

We have a workflow set up to deploy to our PYPI package when a release is created. Here is how you can do that:

  1. Cut a PR for your change
  2. Make sure you increment your version number in the pyproject.toml file in your changes
  3. After approval, merge changes to main branch
  4. Cut a new release in GitHub
    • this can be found on the right hand side of the screen when you are at the repo's home page
    • you should see the current release
  5. For consistency in the release:
    • create a new tag that matches the version number you changed earlier
    • add a title with a brief description of the changes
    • add a small description or link to JIRA tickets for updates
  6. After creating the release you should see a workflow get triggered, this will deploy the updated version to pypi
  7. If you want to see check the pypi package page after the workflow completes running

NOTE: DO NOT change the name of the workflow file. If you do the deployment will not work unless we update the configuration in PYPI under trusted publishers

If you have questions or concerns reach out.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

For issues and questions, please use the GitHub issue tracker.

Credits

Based on the URL normalization functionality from tk-core, extracted and packaged for standalone use.

Metadata

Release files for tk-normalizer 1.4.0

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

Source distribution (sdist)

Source distribution for tk-normalizer 1.4.0
File Size Uploaded Provenance Actions
tk_normalizer-1.4.0.tar.gz 19.2 kB With provenance Details for tk_normalizer-1.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tk-normalizer 1.4.0
File Interpreter ABI Platform Actions
tk_normalizer-1.4.0-py3-none-any.whl Python 3 none any Details for tk_normalizer-1.4.0-py3-none-any.whl

Total release size: 28.7 kB

Release files / tk_normalizer-1.4.0.tar.gz

Download URL tk_normalizer-1.4.0.tar.gz
Size 19.2 kB
Tags Source
SHA-256 checksum
How to use checksums
0b05db6b9f9503035cf9f299def2ec504239ca72dbf88e490273ca9e0fb4c471
BLAKE2b-256 checksum
How to use checksums
b93f677f154544e75515af5fcfdc80e255006bb760b271451b8e0666432f5dc2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.

Transparency log

Release files / tk_normalizer-1.4.0-py3-none-any.whl

Download URL tk_normalizer-1.4.0-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
00b019ca85d7e4e73333c8edc22b368cacb8cadc5e6855fdb7bccb114a87c875
BLAKE2b-256 checksum
How to use checksums
828eb5b5ff0a8ca848d9726ab6af74b17bb49d4883540c51d547996600775087
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.

Transparency log

Security for tk-normalizer 1.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for tk_normalizer-1.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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