yarl — Yet Another URL Library

1.23.0 · active · verified Sat Mar 28

yarl is an immutable, RFC 3986-compliant URL parsing and manipulation library for Python 3. It provides the URL class with automatic percent-encoding/decoding, pathlib-style path operations, query-string helpers, and human-readable representations. The current release is 1.23.0, published March 2026. Releases ship frequently (multiple times per minor version) as part of the aio-libs ecosystem, closely tracking aiohttp.

Warnings

Install

Imports

Quickstart

Parse, inspect, and mutate URLs; build from parts; path-join with /; apply query with %.

from yarl import URL

# Parse an existing URL
url = URL('https://user:pw@api.example.com:8080/v1/items?page=1&q=foo#section')
print(url.scheme)        # 'https'
print(url.host)          # 'api.example.com'  (decoded)
print(url.raw_host)      # IDNA-encoded form
print(url.port)          # 8080 (explicit); None falls back to scheme default
print(url.path)          # '/v1/items'  (percent-decoded)
print(url.raw_path)      # '/v1/items'  (percent-encoded, wire form)
print(url.query)         # MultiDictProxy with parsed key/value pairs
print(url.query_string)  # 'page=1&q=foo'
print(url.fragment)      # 'section'

# Build from components — port must be int, not str
base = URL.build(scheme='https', host='api.example.com', port=8080, path='/v1')
print(base)  # https://api.example.com:8080/v1

# Path-join (like pathlib)
endpoint = base / 'items' / '42'
print(endpoint)  # https://api.example.com:8080/v1/items/42

# Apply a query string with %
with_query = endpoint % {'expand': 'true', 'format': 'json'}
print(with_query)

# Mutation methods return new URL objects (immutable)
updated = endpoint.with_query({'page': '2'}).with_fragment('results')
print(str(updated))      # wire-safe string for HTTP clients
print(updated.human_repr())  # human-readable (non-ASCII decoded)

# Non-ASCII is encoded automatically
unicode_url = URL('https://example.com/пошук?q=кіт')
print(str(unicode_url))      # percent-encoded
print(unicode_url.human_repr())  # readable

view raw JSON →