URLObject

3.0.0 · active · verified Wed Apr 15

URLObject is a utility class for manipulating URLs in Python. The latest version, 3.0.0, aims for a clearer API by focusing on explicit method names rather than relying heavily on operator overrides, building upon its predecessors. It provides an immutable object-oriented interface for parsing, constructing, and modifying URLs and their components. It is actively maintained with a recent release.

Warnings

Install

Imports

Quickstart

Demonstrates creating a URLObject, immutably modifying its components, accessing properties, and an example of handling authentication details using environment variables.

import os
from urlobject import URLObject

# Create a URLObject from a string
url = URLObject("https://github.com/zacharyvoase/urlobject?topic=python&q=docs#api-reference")
print(f"Original URL: {url}")

# URLObject instances are immutable; methods return new objects
modified_url = (
    url.with_scheme('http')
    .with_hostname('example.com')
    .add_query_param('lang', 'en')
    .without_fragment()
)
print(f"Modified URL: {modified_url}")

# Access individual components
print(f"Scheme: {modified_url.scheme}")
print(f"Hostname: {modified_url.hostname}")
print(f"Path: {modified_url.path}")
print(f"Query params: {modified_url.query_list_multidict()}")

# Example with authentication (use environment variables for sensitive data)
auth_url = URLObject("https://api.example.com/data").with_auth(
    os.environ.get('API_USERNAME', 'your_username'),
    os.environ.get('API_PASSWORD', 'your_password')
)
print(f"URL with auth (sensitive parts hidden): {auth_url.without_auth()}")

view raw JSON →