hyperlink library

21.0.0 · active · verified Sun Mar 29

hyperlink is a pure-Python library that provides a featureful, immutable, and semantically correct URL object, adhering to RFC 3986 and RFC 3987 for URIs and IRIs. It emphasizes correctness and ease of use, providing a robust model for URL parsing, construction, and transformation. The current stable version is 21.0.0, with major releases occurring approximately annually.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates parsing a URL from a string, creating modified copies using the immutable API, navigating paths, accessing query parameters, and constructing a URL from individual components.

from hyperlink import parse

# Parse a URL from text
url = parse(u'http://github.com/python-hyper/hyperlink?utm_source=readthedocs')
print(f"Original URL: {url.to_text()}")

# Create a new URL by replacing components (URL objects are immutable)
secure_url = url.replace(scheme=u'https', port=443)
print(f"Secure URL: {secure_url.to_text()}")

# Navigate to a child path or resolve relative paths
org_url = secure_url.click(u'.') # Clicks to the root of the domain
print(f"Org URL: {org_url.to_text()}")

# Access query parameters
utm_source = secure_url.get(u'utm_source')[0]
print(f"UTM Source: {utm_source}")

# Build a URL from components
new_url_from_parts = URL(scheme=u'https', host=u'example.com', path=[u'api', u'v1'], query=((u'id', u'123'),))
print(f"Built from parts: {new_url_from_parts.to_text()}")

view raw JSON →