Skip to content

Commit 1d845e2

Browse files
committed
docs: Improve example for urlparse
Signed-off-by: Christian Clauss <[email protected]>
1 parent 46c8d91 commit 1d845e2

File tree

1 file changed

+39
-30
lines changed

1 file changed

+39
-30
lines changed

Doc/library/urllib.parse.rst

+39-30
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,26 @@ or on combining URL components into a URL string.
4949
present. For example:
5050

5151
>>> from urllib.parse import urlparse
52-
>>> o = urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
52+
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
53+
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
54+
query='query', fragment='fragment')
55+
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
56+
... "highlight=params#url-parsing")
5357
>>> o # doctest: +NORMALIZE_WHITESPACE
54-
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
55-
params='', query='', fragment='')
58+
ParseResult(scheme='http', netloc='docs.python.org:80',
59+
path='/3/library/urllib.parse.html', params='',
60+
query='highlight=params', fragment='url-parsing')
5661
>>> o.scheme
5762
'http'
63+
>>> o.netloc
64+
'docs.python.org:80'
65+
>>> o.hostname
66+
'docs.python.org'
5867
>>> o.port
5968
80
60-
>>> o.geturl()
61-
'http://www.cwi.nl:80/%7Eguido/Python.html'
69+
>>> o.geturl() # doctest: +NORMALIZE_WHITESPACE
70+
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params#
71+
url-parsing'
6272

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

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-
+------------------+-------+--------------------------+----------------------+
105+
+------------------+-------+-------------------------+------------------------+
106+
| Attribute | Index | Value | Value if not present |
107+
+==================+=======+=========================+========================+
108+
| :attr:`scheme` | 0 | URL scheme specifier | *scheme* parameter |
109+
+------------------+-------+-------------------------+------------------------+
110+
| :attr:`netloc` | 1 | Network location part | empty string |
111+
+------------------+-------+-------------------------+------------------------+
112+
| :attr:`path` | 2 | Hierarchical path | empty string |
113+
+------------------+-------+-------------------------+------------------------+
114+
| :attr:`params` | 3 | No longer used | always an empty string |
115+
+------------------+-------+-------------------------+------------------------+
116+
| :attr:`query` | 4 | Query component | empty string |
117+
+------------------+-------+-------------------------+------------------------+
118+
| :attr:`fragment` | 5 | Fragment identifier | empty string |
119+
+------------------+-------+-------------------------+------------------------+
120+
| :attr:`username` | | User name | :const:`None` |
121+
+------------------+-------+-------------------------+------------------------+
122+
| :attr:`password` | | Password | :const:`None` |
123+
+------------------+-------+-------------------------+------------------------+
124+
| :attr:`hostname` | | Host name (lower case) | :const:`None` |
125+
+------------------+-------+-------------------------+------------------------+
126+
| :attr:`port` | | Port number as integer, | :const:`None` |
127+
| | | if present | |
128+
+------------------+-------+-------------------------+------------------------+
120129

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

0 commit comments

Comments
 (0)