Python Glossary Русский

all Python 3urllib.parseterm

urllib.parse.ParseResult

parse results

The result of urlparse(): a named tuple of six fields — scheme, netloc, path, params, query, fragment — plus hostname, port, username and password.

Syntax

ParseResult(scheme, netloc, path, params, query, fragment)

hostname is lower-cased while netloc stays as it was; port is an int or None. An invalid port (99999) does not stop parsing: ValueError appears only when you access .port. The sixth field, params, holds the ';' parameters of the last path segment, which are almost never used. It is a tuple: unpacking and indexing work, but fields cannot be assigned — _replace() returns a new object and geturl() turns it back into a string.

Examples

from urllib.parse import urlparse
r = urlparse('https://Example.COM:8080/a/b;v=1?q=1#top')
print(r.hostname, r.port, r.params)  # → example.com 8080 v=1
print(r._replace(fragment='').geturl())  # → https://Example.COM:8080/a/b;v=1?q=1
from urllib.parse import urlparse
r = urlparse('https://h:99999/')
print(r.netloc)  # → h:99999
try:
    r.port
except ValueError as e:
    print('ValueError:', e)  # → ValueError: Port out of range 0-65535

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy