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
- 1.Import urlparse and parse_qs from urllib.parse.
- 2.Call urlparse() to split the URL into components.
- 3.Access scheme, netloc, path, query via parsed attributes.
- 4.Use parse_qs() to convert query string to a dict.
Expected output
Scheme: https Netloc: example.com Path: /path Query: foo=bar&baz=qux