Python Glossary Русский

all Python 3urllib.parsefunction

urllib.parse.parse_qs()

query string

Parses a query string (a=1&b=2) into a dict where every value is a list of strings, even when the key appears only once.

Syntax

urllib.parse.parse_qs(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace', max_num_fields=None, separator='&')

Values are always lists: parse_qs('a=1')['a'] is ['1'], not '1', and comparing it with '1' gives False. Blank values (b=) are dropped together with the key unless keep_blank_values=True. '+' becomes a space, numbers stay strings. Since 3.10 only '&' separates pairs: ';' no longer does.

Examples

from urllib.parse import parse_qs
q = parse_qs('a=1&a=2&b=&c=x+y')
print(q)  # → {'a': ['1', '2'], 'c': ['x y']}
print(q['c'] == 'x y', q['c'][0])  # → False x y
from urllib.parse import parse_qs
print(parse_qs('b=', keep_blank_values=True))  # → {'b': ['']}
print(parse_qs('a=1;b=2'))  # → {'a': ['1;b=2']}

See also

Open in the full glossarydocs.python.orgReport an inaccuracy