@@ -48,17 +48,29 @@ or on combining URL components into a URL string.
48
48
result, except for a leading slash in the *path * component, which is retained if
49
49
present. For example:
50
50
51
+ .. doctest ::
52
+ :options: +NORMALIZE_WHITESPACE
53
+
51
54
>>> from urllib.parse import urlparse
52
- >>> o = urlparse(' http://www.cwi.nl:80/%7E guido/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')
56
64
>>> o.scheme
57
65
'http'
66
+ >>> o.netloc
67
+ 'docs.python.org:80'
68
+ >>> o.hostname
69
+ 'docs.python.org'
58
70
>>> o.port
59
71
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 '
62
74
63
75
Following the syntax specifications in :rfc: `1808 `, urlparse recognizes
64
76
a netloc only if it is properly introduced by '//'. Otherwise the
@@ -92,31 +104,30 @@ or on combining URL components into a URL string.
92
104
The return value is a :term: `named tuple `, which means that its items can
93
105
be accessed by index or as named attributes, which are:
94
106
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
+ +------------------+-------+-------------------------+------------------------+
120
131
121
132
Reading the :attr: `port ` attribute will raise a :exc: `ValueError ` if
122
133
an invalid port is specified in the URL. See section
0 commit comments