Python Glossary Русский

all Python 3urllib.parsefunction

urllib.parse.urlencode()

query string

Builds an a=1&b=2 query string from a dict or a list of pairs, encoding values with quote_plus().

Syntax

urllib.parse.urlencode(query, doseq=False, safe='', encoding=None, errors=None, quote_via=quote_plus)

A list value without doseq=True becomes the text '[1, 2]' and is encoded as a whole — doseq=True is needed to get n=1&n=2. A space becomes '+'; pass quote_via=quote to get %20. None is not skipped but sent as the string 'None'. The function does not add the leading '?'.

Examples

from urllib.parse import urlencode
print(urlencode({'q': 'a b', 'n': [1, 2]}))  # → q=a+b&n=%5B1%2C+2%5D
print(urlencode({'n': [1, 2]}, doseq=True))  # → n=1&n=2
print(urlencode({'x': None}))  # → x=None
from urllib.parse import urlencode, quote
print(urlencode({'q': 'a b'}, quote_via=quote))  # → q=a%20b

See also

Open in the full glossarydocs.python.orgReport an inaccuracy