Python Glossary Русский

all Python 3urllib.parsefunction

urllib.parse.urlunparse()

URL parsing

Builds a URL back from six parts — a urlparse() result or any tuple of the same length.

Syntax

urllib.parse.urlunparse((scheme, netloc, path, params, query, fragment))

It needs exactly a six-item tuple: five parts from urlsplit() raise ValueError. Empty parts are dropped together with their delimiters, so a bare '?' or '#' disappears — a urlparse→urlunparse round trip does not always return the original string. No encoding is done: a space in query goes into the URL as is, so use urlencode() or quote() first.

Examples

from urllib.parse import urlunparse
print(urlunparse(('https', 'h', '/p', '', 'q=1', 'f')))  # → https://h/p?q=1#f
print(urlunparse(('https', 'h', '/p', '', 'q=a b', '')))  # → https://h/p?q=a b
from urllib.parse import urlunparse
try:
    urlunparse(('https', 'h', '/p', 'q=1', 'f'))
except ValueError as e:
    print('ValueError')  # → ValueError — пять частей вместо шести

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy