Python urllib.parse splits, joins and percent-encodes URLs and query strings
Answer: urlsplit() parses a URL into five components (scheme, netloc, path, query, fragment).
Examples
from urllib.parse import urlsplit, parse_qs, urlencode, quote, quote_plus, urljoin
parts = urlsplit("https://example.com/a/b?q=1&q=2#top")
parts.scheme, parts.netloc, parts.path # ('https', 'example.com', '/a/b')
parse_qs(parts.query) # {'q': ['1', '2']}
urlencode({"q": "hello world", "page": 2}) # 'q=hello+world&page=2'
quote("/El Niño/") # '/El%20Ni%C3%B1o/'
quote_plus("/El Niño/") # '%2FEl+Ni%C3%B1o%2F'
urljoin("http://www.cwi.nl/%7Eguido/Python.html", "FAQ.html")
# 'http://www.cwi.nl/%7Eguido/FAQ.html'
Which function
urlsplit()returns a 5-item named tuple matchingscheme://netloc/path?query#fragment.urlparse()also splits offparams; it is based on obsoleted RFCs, andurlsplit()should generally be used instead.parse_qs()returns unique variable names as keys and lists of values; blank values are ignored unlesskeep_blank_valuesis true.quote_plus()is the default quote_via function, so spaces in values are quoted as a+character. Encode the result to bytes before using it as POST data withurlopen().quote()replaces special characters using the%xxescape, keeps/by default, and never quotes letters, digits and_.-~.
Security notes
- If the second argument to
urljoin()is an absolute URL (starting with//orscheme://), its hostname and/or scheme will be present in the result, so it is generally not secure to useurljoinwith an attacker-controlled url. urlsplit()andurlparse()do not perform validation of inputs; on unusual input they may return empty strings orNoneinstead of raising an exception.- Out-of-range port numbers raise
ValueError(since 3.6), and ASCII newline and tab characters are stripped from the URL (since 3.10).
See also
Claims
- id: C1 claim: urlsplit() should generally be used instead of urlparse(). source: S1 evidence: "urlsplit() should generally be used instead of urlparse() ."
- id: C2 claim: urlsplit and urlparse do not validate inputs. source: S1 evidence: "The urlsplit() and urlparse() APIs do not perform validation of inputs."
- id: C3 claim: Using urljoin with an attacker-controlled url is generally not secure. source: S1 evidence: "it is generally not secure to use urljoin with an attacker-controlled url"
- id: C4 claim: urlsplit parses a URL into 5 components. source: S2 evidence: "Parse a URL into 5 components"
Sources
- [S1] urllib.parse — Parse URLs into components. Python docs. https://docs.python.org/3/library/urllib.parse.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] Lib/urllib/parse.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/urllib/parse.py . License: PSF-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, urllib.parse — Parse URLs into components; python/cpython (GitHub), Lib/urllib/parse.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, urllib.parse — Parse URLs into components; python/cpython (GitHub), Lib/urllib/parse.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python urllib.parse splits, joins and percent-encodes URLs and query strings." iinventedlight Library, iil:note:ztahmsws, version of 2026-11-18. https://iinventedlight.com/library/notes/python-urllib-parse/. License: CC-BY-4.0.
@misc{iil_note_ztahmsws,
author = {{iinventedlight}},
title = {{Python urllib.parse splits, joins and percent-encodes URLs and query strings}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-urllib-parse/},
note = {Permanent ID iil:note:ztahmsws. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-urllib-parse.table.json (table; CC-BY-4.0)
- python-urllib-parse.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-urllib-parse.md.