Python Glossary Русский

all Python 3urllib.parsefunction

urllib.parse.urlparse()

URL parsing

Splits a URL into six parts — scheme, network location, path, params, query and fragment — and returns a ParseResult.

Syntax

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

It differs from urlsplit() by the params field: ';…' in the last path segment goes there and the path gets shorter, which is rarely expected; the docs recommend urlsplit(). An address without '//' ('example.com/x') is parsed as a path and netloc stays empty. Percent-escapes are not decoded, and a malformed URL almost never raises — validation is up to you.

Examples

from urllib.parse import urlparse, urlsplit
print(urlparse('http://h/p;x=1?q=2').path)  # → /p
print(urlsplit('http://h/p;x=1?q=2').path)  # → /p;x=1
from urllib.parse import urlparse
print(repr(urlparse('example.com/x').netloc))  # → ''
print(urlparse('//example.com/x').netloc)  # → example.com

See also

Open in the full glossarydocs.python.orgReport an inaccuracy