Parse URL Components with urllib

Extract scheme, host, path, and query from a URL.

Code

Python
from urllib.parse import urlparse, parse_qs

url = "https://example.com/path?foo=bar&baz=qux"
parsed = urlparse(url)

print("Scheme:", parsed.scheme)
print("Netloc:", parsed.netloc)
print("Path:", parsed.path)
print("Query:", parsed.query)
print("Params:", parse_qs(parsed.query))

Line-by-line explanation

Expected output

Scheme: https
Netloc: example.com
Path: /path
Query: foo=bar&baz=qux

Related snippets

Related DuskTools