百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
U

url-normalize

> 数据库
开源

Python 的 URL 正则化

100 stars0 点赞4 次浏览
访问官网GitHub

工具介绍

Python 的 URL 正则化

url-normalize

A Python library for standardizing and normalizing URLs. Ideal for database deduplication, caching, web crawling, and anywhere you need to ensure that equivalent URLs resolve to the exact same string.

python
from url_normalize import url_normalize

# Fixes IDN, lowercases host/scheme, removes default ports, resolves path segments
url_normalize("HTTP://User:[email protected]:80///foo/../bar/./baz?q=1#frag")
# -> 'http://User:[email protected]/bar/baz?q=1#frag'

Features

url-normalize provides a robust URI normalization function that handles IDN domains, scheme/host lowercasing, and RFC-compliant path normalization.

  • IDN Support: Full internationalized domain name handling (using IDNA2008 with UTS46).
  • Humanization: Convert normalized URLs to a readable display format while preserving round-trip normalization.
  • RFC Compliance:
    • Proper percent-encoding (minimal, uppercase hex).
    • Dot-segment removal in paths.
    • Default port and authority handling.
    • UTF-8 NFC normalization.
  • Configurable Defaults:
    • Customizable default scheme (https by default).
    • Configurable default domain for absolute paths.
  • Query Parameter Control:
    • Parameter filtering with allowlists.
    • Support for domain-specific parameter rules.
  • Versatile URL Handling: Handles empty strings, double-slash URLs (//domain.tld), and shebang (#!) URLs.
  • Developer Friendly:
    • Python 3.10+ compatibility.
    • 100% test coverage.
    • Modern type hints and string handling.

Inspired by Sam Ruby's urlnorm.py.

Installation

Install as a library:

bash
pip install url-normalize

Or install as a standalone CLI tool using uv:

bash
uv tool install url-normalize

Usage

Python API

Basic Normalization

python
from url_normalize import url_normalize

# Basic normalization (uses https by default)
print(url_normalize("www.foo.com:80/foo"))
# Output: https://www.foo.com/foo

# With custom default scheme
print(url_normalize("www.foo.com/foo", default_scheme="http"))
# Output: http://www.foo.com/foo

Query Parameter Filtering

You can strip out tracking parameters and only keep the ones you care about using allowlists.

…

Default Domain & Scheme

Useful for resolving relative URLs found on a specific page.

python
# With default domain for absolute paths
print(url_normalize("/images/logo.png", default_domain="example.com"))
# Output: https://example.com/images/logo.png

# With default domain and custom scheme
print(url_normalize("/images/logo.png", default_scheme="http", default_domain="example.com"))
# Output: http://example.com/images/logo.png

Humanizing URLs

Convert normalized URLs back into a user-friendly format for display, particularly useful for IDN domains and percent-encoded paths.

python
from url_normalize import url_humanize

# Human-readable display form that still normalizes back to the same URL
print(url_humanize("https://xn--e1afmkfd.xn--80akhbyknj4f/%D0%A1%D0%BB%D1%83%D0%B6%D0%B5%D0%B1%D0%BD%D0%B0%D1%8F"))
# Output: https://пример.испытание/Служебная

# Humanization accepts the same normalization options
print(url_humanize("/%D0%A1%D0%BB%D1%83%D0%B6%D0%B5%D0%B1%D0%BD%D0%B0%D1%8F", default_domain="xn--e1afmkfd.xn--80akhbyknj4f"))
# Output: https://пример.испытание/Служебная

Command-line Usage

You can also use url-normalize directly from the terminal to process URLs.

…

Documentation

For a complete history of changes, see CHANGELOG.md.

Contributing

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

License

MIT License

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Pythonnormalizationnormalizenormalizerpython

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库