summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Clauss <cclauss@me.com>2021-12-02 08:52:32 (GMT)
committerGitHub <noreply@github.com>2021-12-02 08:52:32 (GMT)
commit226d22ff2d209495621550eb78e81ed4c0fe0152 (patch)
treebda1a54c4c8cd4673234284146cc1ba89ba3b74e
parent309110f37cdfc78d160ed08ae8faa6f6160ba87e (diff)
downloadcpython-226d22ff2d209495621550eb78e81ed4c0fe0152.zip
cpython-226d22ff2d209495621550eb78e81ed4c0fe0152.tar.gz
cpython-226d22ff2d209495621550eb78e81ed4c0fe0152.tar.bz2
docs: Improve example for urlparse() (GH-29816)
-rw-r--r--Doc/library/urllib.parse.rst73
1 files changed, 42 insertions, 31 deletions
diff --git a/Doc/library/urllib.parse.rst b/Doc/library/urllib.parse.rst
index a060cc9..1478b34 100644
--- a/Doc/library/urllib.parse.rst
+++ b/Doc/library/urllib.parse.rst
@@ -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
present. For example:
+ .. doctest::
+ :options: +NORMALIZE_WHITESPACE
+
>>> from urllib.parse import urlparse
- >>> o = urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
- >>> o # doctest: +NORMALIZE_WHITESPACE
- ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
- params='', query='', fragment='')
+ >>> urlparse("scheme://netloc/path;parameters?query#fragment")
+ ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
+ query='query', fragment='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
'http'
+ >>> o.netloc
+ 'docs.python.org:80'
+ >>> o.hostname
+ 'docs.python.org'
>>> o.port
80
- >>> o.geturl()
- 'http://www.cwi.nl:80/%7Eguido/Python.html'
+ >>> o._replace(fragment="").geturl()
+ 'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
Following the syntax specifications in :rfc:`1808`, urlparse recognizes
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
be accessed by index or as named attributes, which are:
- +------------------+-------+--------------------------+----------------------+
- | Attribute | Index | Value | Value if not present |
- +==================+=======+==========================+======================+
- | :attr:`scheme` | 0 | URL scheme specifier | *scheme* parameter |
- +------------------+-------+--------------------------+----------------------+
- | :attr:`netloc` | 1 | Network location part | empty string |
- +------------------+-------+--------------------------+----------------------+
- | :attr:`path` | 2 | Hierarchical path | empty string |
- +------------------+-------+--------------------------+----------------------+
- | :attr:`params` | 3 | Parameters for last path | empty string |
- | | | element | |
- +------------------+-------+--------------------------+----------------------+
- | :attr:`query` | 4 | Query component | empty string |
- +------------------+-------+--------------------------+----------------------+
- | :attr:`fragment` | 5 | Fragment identifier | empty string |
- +------------------+-------+--------------------------+----------------------+
- | :attr:`username` | | User name | :const:`None` |
- +------------------+-------+--------------------------+----------------------+
- | :attr:`password` | | Password | :const:`None` |
- +------------------+-------+--------------------------+----------------------+
- | :attr:`hostname` | | Host name (lower case) | :const:`None` |
- +------------------+-------+--------------------------+----------------------+
- | :attr:`port` | | Port number as integer, | :const:`None` |
- | | | if present | |
- +------------------+-------+--------------------------+----------------------+
+ +------------------+-------+-------------------------+------------------------+
+ | Attribute | Index | Value | Value if not present |
+ +==================+=======+=========================+========================+
+ | :attr:`scheme` | 0 | URL scheme specifier | *scheme* parameter |
+ +------------------+-------+-------------------------+------------------------+
+ | :attr:`netloc` | 1 | Network location part | empty string |
+ +------------------+-------+-------------------------+------------------------+
+ | :attr:`path` | 2 | Hierarchical path | empty string |
+ +------------------+-------+-------------------------+------------------------+
+ | :attr:`params` | 3 | No longer used | always an empty string |
+ +------------------+-------+-------------------------+------------------------+
+ | :attr:`query` | 4 | Query component | empty string |
+ +------------------+-------+-------------------------+------------------------+
+ | :attr:`fragment` | 5 | Fragment identifier | empty string |
+ +------------------+-------+-------------------------+------------------------+
+ | :attr:`username` | | User name | :const:`None` |
+ +------------------+-------+-------------------------+------------------------+
+ | :attr:`password` | | Password | :const:`None` |
+ +------------------+-------+-------------------------+------------------------+
+ | :attr:`hostname` | | Host name (lower case) | :const:`None` |
+ +------------------+-------+-------------------------+------------------------+
+ | :attr:`port` | | Port number as integer, | :const:`None` |
+ | | | if present | |
+ +------------------+-------+-------------------------+------------------------+
Reading the :attr:`port` attribute will raise a :exc:`ValueError` if
an invalid port is specified in the URL. See section