Step-by-step tutorial on parsing, manipulating, joining, and encoding URLs in Python using standard urllib.parse and modern async libraries like yarl.
Frequently Asked Questions
Q1. What is the difference between urlparse() and urlsplit() in Python?
urlparse() recognizes parameters (;params) as a distinct attribute in older RFC 2396 URLs. urlsplit() follows RFC 3986 and groups path parameters into the path attribute, making it faster and more modern.
Q2. Why does parse_qs() return lists for single values?
In HTTP query strings, keys can appear multiple times (e.g., ?id=1&id=2). To prevent data loss, parse_qs() always maps keys to lists of strings (e.g. {"id": ["1", "2"]}). Use parse_qsl() for a flat list of tuples.
Q3. How do I safely join a relative link with a base URL in Python?
Use urllib.parse.urljoin(base_url, relative_path). For example: urljoin("https://example.com/blog/", "article-1") returns "https://example.com/blog/article-1".