Skip to content

Commit 226d22f

Browse files
authored
docs: Improve example for urlparse() (GH-29816)
1 parent 309110f commit 226d22f

File tree

1 file changed

+42
-31
lines changed

1 file changed

+42
-31
lines changed

Doc/library/urllib.parse.rst

+42-31
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,29 @@ or on combining URL components into a URL string.
4848
result, except for a leading slash in the *path* component, which is retained if
4949
present. For example:
5050

51+
.. doctest::
52+
:options: +NORMALIZE_WHITESPACE
53+
5154
>>> from urllib.parse import urlparse
52-
>>> o = urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
53-
>>> o # doctest: +NORMALIZE_WHITESPACE
54-
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
55-
params='', query='', fragment='')
55+
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
56+
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
57+
query='query', fragment='fragment')
58+
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
59+
... "highlight=params#url-parsing")
60+
>>> o
61+
ParseResult(scheme='http', netloc='docs.python.org:80',
62+
path='/3/library/urllib.parse.html', params='',
63+
query='highlight=params', fragment='url-parsing')
5664
>>> o.scheme
5765
'http'
66+
>>> o.netloc
67+
'docs.python.org:80'
68+
>>> o.hostname
69+
'docs.python.org'
5870
>>> o.port
5971
80
60-
>>> o.geturl()
61-
'http://www.cwi.nl:80/%7Eguido/Python.html'
72+
>>> o._replace(fragment="").geturl()
73+
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
6274

6375
Following the syntax specifications in :rfc:`1808`, urlparse recognizes
6476
a netloc only if it is properly introduced by '//'. Otherwise the
@@ -92,31 +104,30 @@ or on combining URL components into a URL string.
92104
The return value is a :term:`named tuple`, which means that its items can
93105
be accessed by index or as named attributes, which are:
94106

95-
+------------------+-------+--------------------------+----------------------+
96-
| Attribute | Index | Value | Value if not present |
97-
+==================+=======+==========================+======================+
98-
| :attr:`scheme` | 0 | URL scheme specifier | *scheme* parameter |
99-
+------------------+-------+--------------------------+----------------------+
100-
| :attr:`netloc` | 1 | Network location part | empty string |
101-
+------------------+-------+--------------------------+----------------------+
102-
| :attr:`path` | 2 | Hierarchical path | empty string |
103-
+------------------+-------+--------------------------+----------------------+
104-
| :attr:`params` | 3 | Parameters for last path | empty string |
105-
| | | element | |
106-
+------------------+-------+--------------------------+----------------------+
107-
| :attr:`query` | 4 | Query component | empty string |
108-
+------------------+-------+--------------------------+----------------------+
109-
| :attr:`fragment` | 5 | Fragment identifier | empty string |
110-
+------------------+-------+--------------------------+----------------------+
111-
| :attr:`username` | | User name | :const:`None` |
112-
+------------------+-------+--------------------------+----------------------+
113-
| :attr:`password` | | Password | :const:`None` |
114-
+------------------+-------+--------------------------+----------------------+
115-
| :attr:`hostname` | | Host name (lower case) | :const:`None` |
116-
+------------------+-------+--------------------------+----------------------+
117-
| :attr:`port` | | Port number as integer, | :const:`None` |
118-
| | | if present | |
119-
+------------------+-------+--------------------------+----------------------+
107+
+------------------+-------+-------------------------+------------------------+
108+
| Attribute | Index | Value | Value if not present |
109+
+==================+=======+=========================+========================+
110+
| :attr:`scheme` | 0 | URL scheme specifier | *scheme* parameter |
111+
+------------------+-------+-------------------------+------------------------+
112+
| :attr:`netloc` | 1 | Network location part | empty string |
113+
+------------------+-------+-------------------------+------------------------+
114+
| :attr:`path` | 2 | Hierarchical path | empty string |
115+
+------------------+-------+-------------------------+------------------------+
116+
| :attr:`params` | 3 | No longer used | always an empty string |
117+
+------------------+-------+-------------------------+------------------------+
118+
| :attr:`query` | 4 | Query component | empty string |
119+
+------------------+-------+-------------------------+------------------------+
120+
| :attr:`fragment` | 5 | Fragment identifier | empty string |
121+
+------------------+-------+-------------------------+------------------------+
122+
| :attr:`username` | | User name | :const:`None` |
123+
+------------------+-------+-------------------------+------------------------+
124+
| :attr:`password` | | Password | :const:`None` |
125+
+------------------+-------+-------------------------+------------------------+
126+
| :attr:`hostname` | | Host name (lower case) | :const:`None` |
127+
+------------------+-------+-------------------------+------------------------+
128+
| :attr:`port` | | Port number as integer, | :const:`None` |
129+
| | | if present | |
130+
+------------------+-------+-------------------------+------------------------+
120131

121132
Reading the :attr:`port` attribute will raise a :exc:`ValueError` if
122133
an invalid port is specified in the URL. See section

0 commit comments

Comments
 (0)