purl

1.6 · active · verified Mon Apr 13

purl is an immutable URL class designed for easy URL-building and manipulation in Python. It provides a clean API for interrogation and transformation of URLs, aiming to offer a more intuitive experience than the standard library's urlparse and urllib modules. The current version is 1.6, released in May 2021, and the project is actively maintained.

Warnings

Install

Imports

Quickstart

Demonstrates how to construct a URL object, access its various components, and perform modifications. Note that all modification methods return a new URL instance due to the library's immutable design.

from purl import URL

# Constructing a URL
u = URL('https://www.example.com/path/to/resource?id=123&lang=en#section-1')
print(f"Original URL: {u}")

# Accessing parts of the URL
print(f"Scheme: {u.scheme()}")
print(f"Host: {u.host()}")
print(f"Path: {u.path()}")
print(f"Query parameter 'id': {u.query_param('id')}")

# Modifying the URL (returns a *new* URL object)
new_u = u.query_param('lang', 'fr').remove_query_param('id').fragment('top')
print(f"Modified URL: {new_u}")
print(f"Original URL (unchanged): {u}")

# Chaining operations
chained_url = URL('http://api.example.com').path('v1').path_segment(1, 'users').query_param('sort', 'asc')
print(f"Chained URL: {chained_url}")

view raw JSON →