docs: Improve example for urlparse() (GH-29816)

This commit is contained in:
Christian Clauss 2021-12-02 09:52:32 +01:00 committed by GitHub
parent 309110f37c
commit 226d22ff2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -48,17 +48,29 @@ or on combining URL components into a URL string.
result, except for a leading slash in the *path* component, which is retained if result, except for a leading slash in the *path* component, which is retained if
present. For example: present. For example:
.. doctest::
:options: +NORMALIZE_WHITESPACE
>>> from urllib.parse import urlparse >>> from urllib.parse import urlparse
>>> o = urlparse('http://www.cwi.nl:80/%7Eguido/Python.html') >>> urlparse("scheme://netloc/path;parameters?query#fragment")
>>> o # doctest: +NORMALIZE_WHITESPACE ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html', query='query', fragment='fragment')
params='', query='', fragment='') >>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
... "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
path='/3/library/urllib.parse.html', params='',
query='highlight=params', fragment='url-parsing')
>>> o.scheme >>> o.scheme
'http' 'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port >>> o.port
80 80
>>> o.geturl() >>> o._replace(fragment="").geturl()
'http://www.cwi.nl:80/%7Eguido/Python.html' 'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
Following the syntax specifications in :rfc:`1808`, urlparse recognizes Following the syntax specifications in :rfc:`1808`, urlparse recognizes
a netloc only if it is properly introduced by '//'. Otherwise the a netloc only if it is properly introduced by '//'. Otherwise the
@ -92,31 +104,30 @@ or on combining URL components into a URL string.
The return value is a :term:`named tuple`, which means that its items can The return value is a :term:`named tuple`, which means that its items can
be accessed by index or as named attributes, which are: be accessed by index or as named attributes, which are:
+------------------+-------+--------------------------+----------------------+ +------------------+-------+-------------------------+------------------------+
| Attribute | Index | Value | Value if not present | | Attribute | Index | Value | Value if not present |
+==================+=======+==========================+======================+ +==================+=======+=========================+========================+
| :attr:`scheme` | 0 | URL scheme specifier | *scheme* parameter | | :attr:`scheme` | 0 | URL scheme specifier | *scheme* parameter |
+------------------+-------+--------------------------+----------------------+ +------------------+-------+-------------------------+------------------------+
| :attr:`netloc` | 1 | Network location part | empty string | | :attr:`netloc` | 1 | Network location part | empty string |
+------------------+-------+--------------------------+----------------------+ +------------------+-------+-------------------------+------------------------+
| :attr:`path` | 2 | Hierarchical path | empty string | | :attr:`path` | 2 | Hierarchical path | empty string |
+------------------+-------+--------------------------+----------------------+ +------------------+-------+-------------------------+------------------------+
| :attr:`params` | 3 | Parameters for last path | empty string | | :attr:`params` | 3 | No longer used | always an empty string |
| | | element | | +------------------+-------+-------------------------+------------------------+
+------------------+-------+--------------------------+----------------------+
| :attr:`query` | 4 | Query component | empty string | | :attr:`query` | 4 | Query component | empty string |
+------------------+-------+--------------------------+----------------------+ +------------------+-------+-------------------------+------------------------+
| :attr:`fragment` | 5 | Fragment identifier | empty string | | :attr:`fragment` | 5 | Fragment identifier | empty string |
+------------------+-------+--------------------------+----------------------+ +------------------+-------+-------------------------+------------------------+
| :attr:`username` | | User name | :const:`None` | | :attr:`username` | | User name | :const:`None` |
+------------------+-------+--------------------------+----------------------+ +------------------+-------+-------------------------+------------------------+
| :attr:`password` | | Password | :const:`None` | | :attr:`password` | | Password | :const:`None` |
+------------------+-------+--------------------------+----------------------+ +------------------+-------+-------------------------+------------------------+
| :attr:`hostname` | | Host name (lower case) | :const:`None` | | :attr:`hostname` | | Host name (lower case) | :const:`None` |
+------------------+-------+--------------------------+----------------------+ +------------------+-------+-------------------------+------------------------+
| :attr:`port` | | Port number as integer, | :const:`None` | | :attr:`port` | | Port number as integer, | :const:`None` |
| | | if present | | | | | if present | |
+------------------+-------+--------------------------+----------------------+ +------------------+-------+-------------------------+------------------------+
Reading the :attr:`port` attribute will raise a :exc:`ValueError` if Reading the :attr:`port` attribute will raise a :exc:`ValueError` if
an invalid port is specified in the URL. See section an invalid port is specified in the URL. See section