Python Glossary Русский

all Python 3urllib.parsefunction

urllib.parse.urlsplit()

URL parsing

Splits a URL into five parts — scheme, network location, path, query and fragment — and returns a SplitResult; the recommended choice over urlparse().

Syntax

urllib.parse.urlsplit(urlstring, scheme='', allow_fragments=True)

Missing parts are empty strings, so a bare '?' and no '?' at all look the same; 3.15 added missing_as_none=True for that. The scheme is lower-cased, the host in netloc is not (.hostname does that). The port is checked lazily: ValueError appears when .port is read. Leading spaces and control characters, and newlines inside the address, are silently stripped.

Examples

from urllib.parse import urlsplit
r = urlsplit('HTTPS://Example.com:8443/p?q=1#f')
print(r.scheme, r.netloc, r.hostname, r.port)  # → https Example.com:8443 example.com 8443
print(urlsplit('http://h/p?') == urlsplit('http://h/p'))  # → True

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy