Home | History | Annotate | Download | only in library
      1 :mod:`urllib.parse` --- Parse URLs into components
      2 ==================================================
      3 
      4 .. module:: urllib.parse
      5    :synopsis: Parse URLs into or assemble them from components.
      6 
      7 **Source code:** :source:`Lib/urllib/parse.py`
      8 
      9 .. index::
     10    single: WWW
     11    single: World Wide Web
     12    single: URL
     13    pair: URL; parsing
     14    pair: relative; URL
     15 
     16 --------------
     17 
     18 This module defines a standard interface to break Uniform Resource Locator (URL)
     19 strings up in components (addressing scheme, network location, path etc.), to
     20 combine the components back into a URL string, and to convert a "relative URL"
     21 to an absolute URL given a "base URL."
     22 
     23 The module has been designed to match the Internet RFC on Relative Uniform
     24 Resource Locators. It supports the following URL schemes: ``file``, ``ftp``,
     25 ``gopher``, ``hdl``, ``http``, ``https``, ``imap``, ``mailto``, ``mms``,
     26 ``news``, ``nntp``, ``prospero``, ``rsync``, ``rtsp``, ``rtspu``, ``sftp``,
     27 ``shttp``, ``sip``, ``sips``, ``snews``, ``svn``, ``svn+ssh``, ``telnet``,
     28 ``wais``, ``ws``, ``wss``.
     29 
     30 The :mod:`urllib.parse` module defines functions that fall into two broad
     31 categories: URL parsing and URL quoting. These are covered in detail in
     32 the following sections.
     33 
     34 URL Parsing
     35 -----------
     36 
     37 The URL parsing functions focus on splitting a URL string into its components,
     38 or on combining URL components into a URL string.
     39 
     40 .. function:: urlparse(urlstring, scheme='', allow_fragments=True)
     41 
     42    Parse a URL into six components, returning a 6-tuple.  This corresponds to the
     43    general structure of a URL: ``scheme://netloc/path;parameters?query#fragment``.
     44    Each tuple item is a string, possibly empty. The components are not broken up in
     45    smaller parts (for example, the network location is a single string), and %
     46    escapes are not expanded. The delimiters as shown above are not part of the
     47    result, except for a leading slash in the *path* component, which is retained if
     48    present.  For example:
     49 
     50       >>> from urllib.parse import urlparse
     51       >>> o = urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
     52       >>> o   # doctest: +NORMALIZE_WHITESPACE
     53       ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
     54                   params='', query='', fragment='')
     55       >>> o.scheme
     56       'http'
     57       >>> o.port
     58       80
     59       >>> o.geturl()
     60       'http://www.cwi.nl:80/%7Eguido/Python.html'
     61 
     62    Following the syntax specifications in :rfc:`1808`, urlparse recognizes
     63    a netloc only if it is properly introduced by '//'.  Otherwise the
     64    input is presumed to be a relative URL and thus to start with
     65    a path component.
     66 
     67        >>> from urllib.parse import urlparse
     68        >>> urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
     69        ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
     70                   params='', query='', fragment='')
     71        >>> urlparse('www.cwi.nl/%7Eguido/Python.html')
     72        ParseResult(scheme='', netloc='', path='www.cwi.nl/%7Eguido/Python.html',
     73                   params='', query='', fragment='')
     74        >>> urlparse('help/Python.html')
     75        ParseResult(scheme='', netloc='', path='help/Python.html', params='',
     76                   query='', fragment='')
     77 
     78    The *scheme* argument gives the default addressing scheme, to be
     79    used only if the URL does not specify one.  It should be the same type
     80    (text or bytes) as *urlstring*, except that the default value ``''`` is
     81    always allowed, and is automatically converted to ``b''`` if appropriate.
     82 
     83    If the *allow_fragments* argument is false, fragment identifiers are not
     84    recognized.  Instead, they are parsed as part of the path, parameters
     85    or query component, and :attr:`fragment` is set to the empty string in
     86    the return value.
     87 
     88    The return value is actually an instance of a subclass of :class:`tuple`.  This
     89    class has the following additional read-only convenience attributes:
     90 
     91    +------------------+-------+--------------------------+----------------------+
     92    | Attribute        | Index | Value                    | Value if not present |
     93    +==================+=======+==========================+======================+
     94    | :attr:`scheme`   | 0     | URL scheme specifier     | *scheme* parameter   |
     95    +------------------+-------+--------------------------+----------------------+
     96    | :attr:`netloc`   | 1     | Network location part    | empty string         |
     97    +------------------+-------+--------------------------+----------------------+
     98    | :attr:`path`     | 2     | Hierarchical path        | empty string         |
     99    +------------------+-------+--------------------------+----------------------+
    100    | :attr:`params`   | 3     | Parameters for last path | empty string         |
    101    |                  |       | element                  |                      |
    102    +------------------+-------+--------------------------+----------------------+
    103    | :attr:`query`    | 4     | Query component          | empty string         |
    104    +------------------+-------+--------------------------+----------------------+
    105    | :attr:`fragment` | 5     | Fragment identifier      | empty string         |
    106    +------------------+-------+--------------------------+----------------------+
    107    | :attr:`username` |       | User name                | :const:`None`        |
    108    +------------------+-------+--------------------------+----------------------+
    109    | :attr:`password` |       | Password                 | :const:`None`        |
    110    +------------------+-------+--------------------------+----------------------+
    111    | :attr:`hostname` |       | Host name (lower case)   | :const:`None`        |
    112    +------------------+-------+--------------------------+----------------------+
    113    | :attr:`port`     |       | Port number as integer,  | :const:`None`        |
    114    |                  |       | if present               |                      |
    115    +------------------+-------+--------------------------+----------------------+
    116 
    117    Reading the :attr:`port` attribute will raise a :exc:`ValueError` if
    118    an invalid port is specified in the URL.  See section
    119    :ref:`urlparse-result-object` for more information on the result object.
    120 
    121    .. versionchanged:: 3.2
    122       Added IPv6 URL parsing capabilities.
    123 
    124    .. versionchanged:: 3.3
    125       The fragment is now parsed for all URL schemes (unless *allow_fragment* is
    126       false), in accordance with :rfc:`3986`.  Previously, a whitelist of
    127       schemes that support fragments existed.
    128 
    129    .. versionchanged:: 3.6
    130       Out-of-range port numbers now raise :exc:`ValueError`, instead of
    131       returning :const:`None`.
    132 
    133 
    134 .. function:: parse_qs(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace')
    135 
    136    Parse a query string given as a string argument (data of type
    137    :mimetype:`application/x-www-form-urlencoded`).  Data are returned as a
    138    dictionary.  The dictionary keys are the unique query variable names and the
    139    values are lists of values for each name.
    140 
    141    The optional argument *keep_blank_values* is a flag indicating whether blank
    142    values in percent-encoded queries should be treated as blank strings. A true value
    143    indicates that blanks should be retained as  blank strings.  The default false
    144    value indicates that blank values are to be ignored and treated as if they were
    145    not included.
    146 
    147    The optional argument *strict_parsing* is a flag indicating what to do with
    148    parsing errors.  If false (the default), errors are silently ignored.  If true,
    149    errors raise a :exc:`ValueError` exception.
    150 
    151    The optional *encoding* and *errors* parameters specify how to decode
    152    percent-encoded sequences into Unicode characters, as accepted by the
    153    :meth:`bytes.decode` method.
    154 
    155    Use the :func:`urllib.parse.urlencode` function (with the ``doseq``
    156    parameter set to ``True``) to convert such dictionaries into query
    157    strings.
    158 
    159 
    160    .. versionchanged:: 3.2
    161       Add *encoding* and *errors* parameters.
    162 
    163 
    164 .. function:: parse_qsl(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace')
    165 
    166    Parse a query string given as a string argument (data of type
    167    :mimetype:`application/x-www-form-urlencoded`).  Data are returned as a list of
    168    name, value pairs.
    169 
    170    The optional argument *keep_blank_values* is a flag indicating whether blank
    171    values in percent-encoded queries should be treated as blank strings. A true value
    172    indicates that blanks should be retained as  blank strings.  The default false
    173    value indicates that blank values are to be ignored and treated as if they were
    174    not included.
    175 
    176    The optional argument *strict_parsing* is a flag indicating what to do with
    177    parsing errors.  If false (the default), errors are silently ignored.  If true,
    178    errors raise a :exc:`ValueError` exception.
    179 
    180    The optional *encoding* and *errors* parameters specify how to decode
    181    percent-encoded sequences into Unicode characters, as accepted by the
    182    :meth:`bytes.decode` method.
    183 
    184    Use the :func:`urllib.parse.urlencode` function to convert such lists of pairs into
    185    query strings.
    186 
    187    .. versionchanged:: 3.2
    188       Add *encoding* and *errors* parameters.
    189 
    190 
    191 .. function:: urlunparse(parts)
    192 
    193    Construct a URL from a tuple as returned by ``urlparse()``. The *parts*
    194    argument can be any six-item iterable. This may result in a slightly
    195    different, but equivalent URL, if the URL that was parsed originally had
    196    unnecessary delimiters (for example, a ``?`` with an empty query; the RFC
    197    states that these are equivalent).
    198 
    199 
    200 .. function:: urlsplit(urlstring, scheme='', allow_fragments=True)
    201 
    202    This is similar to :func:`urlparse`, but does not split the params from the URL.
    203    This should generally be used instead of :func:`urlparse` if the more recent URL
    204    syntax allowing parameters to be applied to each segment of the *path* portion
    205    of the URL (see :rfc:`2396`) is wanted.  A separate function is needed to
    206    separate the path segments and parameters.  This function returns a 5-tuple:
    207    (addressing scheme, network location, path, query, fragment identifier).
    208 
    209    The return value is actually an instance of a subclass of :class:`tuple`.  This
    210    class has the following additional read-only convenience attributes:
    211 
    212    +------------------+-------+-------------------------+----------------------+
    213    | Attribute        | Index | Value                   | Value if not present |
    214    +==================+=======+=========================+======================+
    215    | :attr:`scheme`   | 0     | URL scheme specifier    | *scheme* parameter   |
    216    +------------------+-------+-------------------------+----------------------+
    217    | :attr:`netloc`   | 1     | Network location part   | empty string         |
    218    +------------------+-------+-------------------------+----------------------+
    219    | :attr:`path`     | 2     | Hierarchical path       | empty string         |
    220    +------------------+-------+-------------------------+----------------------+
    221    | :attr:`query`    | 3     | Query component         | empty string         |
    222    +------------------+-------+-------------------------+----------------------+
    223    | :attr:`fragment` | 4     | Fragment identifier     | empty string         |
    224    +------------------+-------+-------------------------+----------------------+
    225    | :attr:`username` |       | User name               | :const:`None`        |
    226    +------------------+-------+-------------------------+----------------------+
    227    | :attr:`password` |       | Password                | :const:`None`        |
    228    +------------------+-------+-------------------------+----------------------+
    229    | :attr:`hostname` |       | Host name (lower case)  | :const:`None`        |
    230    +------------------+-------+-------------------------+----------------------+
    231    | :attr:`port`     |       | Port number as integer, | :const:`None`        |
    232    |                  |       | if present              |                      |
    233    +------------------+-------+-------------------------+----------------------+
    234 
    235    Reading the :attr:`port` attribute will raise a :exc:`ValueError` if
    236    an invalid port is specified in the URL.  See section
    237    :ref:`urlparse-result-object` for more information on the result object.
    238 
    239    .. versionchanged:: 3.6
    240       Out-of-range port numbers now raise :exc:`ValueError`, instead of
    241       returning :const:`None`.
    242 
    243 
    244 .. function:: urlunsplit(parts)
    245 
    246    Combine the elements of a tuple as returned by :func:`urlsplit` into a
    247    complete URL as a string. The *parts* argument can be any five-item
    248    iterable. This may result in a slightly different, but equivalent URL, if the
    249    URL that was parsed originally had unnecessary delimiters (for example, a ?
    250    with an empty query; the RFC states that these are equivalent).
    251 
    252 
    253 .. function:: urljoin(base, url, allow_fragments=True)
    254 
    255    Construct a full ("absolute") URL by combining a "base URL" (*base*) with
    256    another URL (*url*).  Informally, this uses components of the base URL, in
    257    particular the addressing scheme, the network location and (part of) the
    258    path, to provide missing components in the relative URL.  For example:
    259 
    260       >>> from urllib.parse import urljoin
    261       >>> urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html')
    262       'http://www.cwi.nl/%7Eguido/FAQ.html'
    263 
    264    The *allow_fragments* argument has the same meaning and default as for
    265    :func:`urlparse`.
    266 
    267    .. note::
    268 
    269       If *url* is an absolute URL (that is, starting with ``//`` or ``scheme://``),
    270       the *url*'s host name and/or scheme will be present in the result.  For example:
    271 
    272    .. doctest::
    273 
    274       >>> urljoin('http://www.cwi.nl/%7Eguido/Python.html',
    275       ...         '//www.python.org/%7Eguido')
    276       'http://www.python.org/%7Eguido'
    277 
    278    If you do not want that behavior, preprocess the *url* with :func:`urlsplit` and
    279    :func:`urlunsplit`, removing possible *scheme* and *netloc* parts.
    280 
    281 
    282    .. versionchanged:: 3.5
    283 
    284       Behaviour updated to match the semantics defined in :rfc:`3986`.
    285 
    286 
    287 .. function:: urldefrag(url)
    288 
    289    If *url* contains a fragment identifier, return a modified version of *url*
    290    with no fragment identifier, and the fragment identifier as a separate
    291    string.  If there is no fragment identifier in *url*, return *url* unmodified
    292    and an empty string.
    293 
    294    The return value is actually an instance of a subclass of :class:`tuple`.  This
    295    class has the following additional read-only convenience attributes:
    296 
    297    +------------------+-------+-------------------------+----------------------+
    298    | Attribute        | Index | Value                   | Value if not present |
    299    +==================+=======+=========================+======================+
    300    | :attr:`url`      | 0     | URL with no fragment    | empty string         |
    301    +------------------+-------+-------------------------+----------------------+
    302    | :attr:`fragment` | 1     | Fragment identifier     | empty string         |
    303    +------------------+-------+-------------------------+----------------------+
    304 
    305    See section :ref:`urlparse-result-object` for more information on the result
    306    object.
    307 
    308    .. versionchanged:: 3.2
    309       Result is a structured object rather than a simple 2-tuple.
    310 
    311 .. _parsing-ascii-encoded-bytes:
    312 
    313 Parsing ASCII Encoded Bytes
    314 ---------------------------
    315 
    316 The URL parsing functions were originally designed to operate on character
    317 strings only. In practice, it is useful to be able to manipulate properly
    318 quoted and encoded URLs as sequences of ASCII bytes. Accordingly, the
    319 URL parsing functions in this module all operate on :class:`bytes` and
    320 :class:`bytearray` objects in addition to :class:`str` objects.
    321 
    322 If :class:`str` data is passed in, the result will also contain only
    323 :class:`str` data. If :class:`bytes` or :class:`bytearray` data is
    324 passed in, the result will contain only :class:`bytes` data.
    325 
    326 Attempting to mix :class:`str` data with :class:`bytes` or
    327 :class:`bytearray` in a single function call will result in a
    328 :exc:`TypeError` being raised, while attempting to pass in non-ASCII
    329 byte values will trigger :exc:`UnicodeDecodeError`.
    330 
    331 To support easier conversion of result objects between :class:`str` and
    332 :class:`bytes`, all return values from URL parsing functions provide
    333 either an :meth:`encode` method (when the result contains :class:`str`
    334 data) or a :meth:`decode` method (when the result contains :class:`bytes`
    335 data). The signatures of these methods match those of the corresponding
    336 :class:`str` and :class:`bytes` methods (except that the default encoding
    337 is ``'ascii'`` rather than ``'utf-8'``). Each produces a value of a
    338 corresponding type that contains either :class:`bytes` data (for
    339 :meth:`encode` methods) or :class:`str` data (for
    340 :meth:`decode` methods).
    341 
    342 Applications that need to operate on potentially improperly quoted URLs
    343 that may contain non-ASCII data will need to do their own decoding from
    344 bytes to characters before invoking the URL parsing methods.
    345 
    346 The behaviour described in this section applies only to the URL parsing
    347 functions. The URL quoting functions use their own rules when producing
    348 or consuming byte sequences as detailed in the documentation of the
    349 individual URL quoting functions.
    350 
    351 .. versionchanged:: 3.2
    352    URL parsing functions now accept ASCII encoded byte sequences
    353 
    354 
    355 .. _urlparse-result-object:
    356 
    357 Structured Parse Results
    358 ------------------------
    359 
    360 The result objects from the :func:`urlparse`, :func:`urlsplit`  and
    361 :func:`urldefrag` functions are subclasses of the :class:`tuple` type.
    362 These subclasses add the attributes listed in the documentation for
    363 those functions, the encoding and decoding support described in the
    364 previous section, as well as an additional method:
    365 
    366 .. method:: urllib.parse.SplitResult.geturl()
    367 
    368    Return the re-combined version of the original URL as a string. This may
    369    differ from the original URL in that the scheme may be normalized to lower
    370    case and empty components may be dropped. Specifically, empty parameters,
    371    queries, and fragment identifiers will be removed.
    372 
    373    For :func:`urldefrag` results, only empty fragment identifiers will be removed.
    374    For :func:`urlsplit` and :func:`urlparse` results, all noted changes will be
    375    made to the URL returned by this method.
    376 
    377    The result of this method remains unchanged if passed back through the original
    378    parsing function:
    379 
    380       >>> from urllib.parse import urlsplit
    381       >>> url = 'HTTP://www.Python.org/doc/#'
    382       >>> r1 = urlsplit(url)
    383       >>> r1.geturl()
    384       'http://www.Python.org/doc/'
    385       >>> r2 = urlsplit(r1.geturl())
    386       >>> r2.geturl()
    387       'http://www.Python.org/doc/'
    388 
    389 
    390 The following classes provide the implementations of the structured parse
    391 results when operating on :class:`str` objects:
    392 
    393 .. class:: DefragResult(url, fragment)
    394 
    395    Concrete class for :func:`urldefrag` results containing :class:`str`
    396    data. The :meth:`encode` method returns a :class:`DefragResultBytes`
    397    instance.
    398 
    399    .. versionadded:: 3.2
    400 
    401 .. class:: ParseResult(scheme, netloc, path, params, query, fragment)
    402 
    403    Concrete class for :func:`urlparse` results containing :class:`str`
    404    data. The :meth:`encode` method returns a :class:`ParseResultBytes`
    405    instance.
    406 
    407 .. class:: SplitResult(scheme, netloc, path, query, fragment)
    408 
    409    Concrete class for :func:`urlsplit` results containing :class:`str`
    410    data. The :meth:`encode` method returns a :class:`SplitResultBytes`
    411    instance.
    412 
    413 
    414 The following classes provide the implementations of the parse results when
    415 operating on :class:`bytes` or :class:`bytearray` objects:
    416 
    417 .. class:: DefragResultBytes(url, fragment)
    418 
    419    Concrete class for :func:`urldefrag` results containing :class:`bytes`
    420    data. The :meth:`decode` method returns a :class:`DefragResult`
    421    instance.
    422 
    423    .. versionadded:: 3.2
    424 
    425 .. class:: ParseResultBytes(scheme, netloc, path, params, query, fragment)
    426 
    427    Concrete class for :func:`urlparse` results containing :class:`bytes`
    428    data. The :meth:`decode` method returns a :class:`ParseResult`
    429    instance.
    430 
    431    .. versionadded:: 3.2
    432 
    433 .. class:: SplitResultBytes(scheme, netloc, path, query, fragment)
    434 
    435    Concrete class for :func:`urlsplit` results containing :class:`bytes`
    436    data. The :meth:`decode` method returns a :class:`SplitResult`
    437    instance.
    438 
    439    .. versionadded:: 3.2
    440 
    441 
    442 URL Quoting
    443 -----------
    444 
    445 The URL quoting functions focus on taking program data and making it safe
    446 for use as URL components by quoting special characters and appropriately
    447 encoding non-ASCII text. They also support reversing these operations to
    448 recreate the original data from the contents of a URL component if that
    449 task isn't already covered by the URL parsing functions above.
    450 
    451 .. function:: quote(string, safe='/', encoding=None, errors=None)
    452 
    453    Replace special characters in *string* using the ``%xx`` escape. Letters,
    454    digits, and the characters ``'_.-'`` are never quoted. By default, this
    455    function is intended for quoting the path section of URL. The optional *safe*
    456    parameter specifies additional ASCII characters that should not be quoted
    457    --- its default value is ``'/'``.
    458 
    459    *string* may be either a :class:`str` or a :class:`bytes`.
    460 
    461    The optional *encoding* and *errors* parameters specify how to deal with
    462    non-ASCII characters, as accepted by the :meth:`str.encode` method.
    463    *encoding* defaults to ``'utf-8'``.
    464    *errors* defaults to ``'strict'``, meaning unsupported characters raise a
    465    :class:`UnicodeEncodeError`.
    466    *encoding* and *errors* must not be supplied if *string* is a
    467    :class:`bytes`, or a :class:`TypeError` is raised.
    468 
    469    Note that ``quote(string, safe, encoding, errors)`` is equivalent to
    470    ``quote_from_bytes(string.encode(encoding, errors), safe)``.
    471 
    472    Example: ``quote('/El Nio/')`` yields ``'/El%20Ni%C3%B1o/'``.
    473 
    474 
    475 .. function:: quote_plus(string, safe='', encoding=None, errors=None)
    476 
    477    Like :func:`quote`, but also replace spaces by plus signs, as required for
    478    quoting HTML form values when building up a query string to go into a URL.
    479    Plus signs in the original string are escaped unless they are included in
    480    *safe*.  It also does not have *safe* default to ``'/'``.
    481 
    482    Example: ``quote_plus('/El Nio/')`` yields ``'%2FEl+Ni%C3%B1o%2F'``.
    483 
    484 
    485 .. function:: quote_from_bytes(bytes, safe='/')
    486 
    487    Like :func:`quote`, but accepts a :class:`bytes` object rather than a
    488    :class:`str`, and does not perform string-to-bytes encoding.
    489 
    490    Example: ``quote_from_bytes(b'a&\xef')`` yields
    491    ``'a%26%EF'``.
    492 
    493 
    494 .. function:: unquote(string, encoding='utf-8', errors='replace')
    495 
    496    Replace ``%xx`` escapes by their single-character equivalent.
    497    The optional *encoding* and *errors* parameters specify how to decode
    498    percent-encoded sequences into Unicode characters, as accepted by the
    499    :meth:`bytes.decode` method.
    500 
    501    *string* must be a :class:`str`.
    502 
    503    *encoding* defaults to ``'utf-8'``.
    504    *errors* defaults to ``'replace'``, meaning invalid sequences are replaced
    505    by a placeholder character.
    506 
    507    Example: ``unquote('/El%20Ni%C3%B1o/')`` yields ``'/El Nio/'``.
    508 
    509 
    510 .. function:: unquote_plus(string, encoding='utf-8', errors='replace')
    511 
    512    Like :func:`unquote`, but also replace plus signs by spaces, as required for
    513    unquoting HTML form values.
    514 
    515    *string* must be a :class:`str`.
    516 
    517    Example: ``unquote_plus('/El+Ni%C3%B1o/')`` yields ``'/El Nio/'``.
    518 
    519 
    520 .. function:: unquote_to_bytes(string)
    521 
    522    Replace ``%xx`` escapes by their single-octet equivalent, and return a
    523    :class:`bytes` object.
    524 
    525    *string* may be either a :class:`str` or a :class:`bytes`.
    526 
    527    If it is a :class:`str`, unescaped non-ASCII characters in *string*
    528    are encoded into UTF-8 bytes.
    529 
    530    Example: ``unquote_to_bytes('a%26%EF')`` yields ``b'a&\xef'``.
    531 
    532 
    533 .. function:: urlencode(query, doseq=False, safe='', encoding=None, \
    534                         errors=None, quote_via=quote_plus)
    535 
    536    Convert a mapping object or a sequence of two-element tuples, which may
    537    contain :class:`str` or :class:`bytes` objects, to a percent-encoded ASCII
    538    text string.  If the resultant string is to be used as a *data* for POST
    539    operation with the :func:`~urllib.request.urlopen` function, then
    540    it should be encoded to bytes, otherwise it would result in a
    541    :exc:`TypeError`.
    542 
    543    The resulting string is a series of ``key=value`` pairs separated by ``'&'``
    544    characters, where both *key* and *value* are quoted using the *quote_via*
    545    function.  By default, :func:`quote_plus` is used to quote the values, which
    546    means spaces are quoted as a ``'+'`` character and '/' characters are
    547    encoded as ``%2F``, which follows the standard for GET requests
    548    (``application/x-www-form-urlencoded``).  An alternate function that can be
    549    passed as *quote_via* is :func:`quote`, which will encode spaces as ``%20``
    550    and not encode '/' characters.  For maximum control of what is quoted, use
    551    ``quote`` and specify a value for *safe*.
    552 
    553    When a sequence of two-element tuples is used as the *query*
    554    argument, the first element of each tuple is a key and the second is a
    555    value. The value element in itself can be a sequence and in that case, if
    556    the optional parameter *doseq* is evaluates to ``True``, individual
    557    ``key=value`` pairs separated by ``'&'`` are generated for each element of
    558    the value sequence for the key.  The order of parameters in the encoded
    559    string will match the order of parameter tuples in the sequence.
    560 
    561    The *safe*, *encoding*, and *errors* parameters are passed down to
    562    *quote_via* (the *encoding* and *errors* parameters are only passed
    563    when a query element is a :class:`str`).
    564 
    565    To reverse this encoding process, :func:`parse_qs` and :func:`parse_qsl` are
    566    provided in this module to parse query strings into Python data structures.
    567 
    568    Refer to :ref:`urllib examples <urllib-examples>` to find out how urlencode
    569    method can be used for generating query string for a URL or data for POST.
    570 
    571    .. versionchanged:: 3.2
    572       Query parameter supports bytes and string objects.
    573 
    574    .. versionadded:: 3.5
    575       *quote_via* parameter.
    576 
    577 
    578 .. seealso::
    579 
    580    :rfc:`3986` - Uniform Resource Identifiers
    581       This is the current standard (STD66). Any changes to urllib.parse module
    582       should conform to this. Certain deviations could be observed, which are
    583       mostly for backward compatibility purposes and for certain de-facto
    584       parsing requirements as commonly observed in major browsers.
    585 
    586    :rfc:`2732` - Format for Literal IPv6 Addresses in URL's.
    587       This specifies the parsing requirements of IPv6 URLs.
    588 
    589    :rfc:`2396` - Uniform Resource Identifiers (URI): Generic Syntax
    590       Document describing the generic syntactic requirements for both Uniform Resource
    591       Names (URNs) and Uniform Resource Locators (URLs).
    592 
    593    :rfc:`2368` - The mailto URL scheme.
    594       Parsing requirements for mailto URL schemes.
    595 
    596    :rfc:`1808` - Relative Uniform Resource Locators
    597       This Request For Comments includes the rules for joining an absolute and a
    598       relative URL, including a fair number of "Abnormal Examples" which govern the
    599       treatment of border cases.
    600 
    601    :rfc:`1738` - Uniform Resource Locators (URL)
    602       This specifies the formal syntax and semantics of absolute URLs.
    603