rest25/library/urlparse.rst => rest262/library/urlparse.rst
n1- 
2:mod:`urlparse` --- Parse URLs into components
3==============================================
4
5.. module:: urlparse
n6- 
n5+   :synopsis: Parse URLs into or assemble them from components.
7- 
8
9
10.. index::
11   single: WWW
12   single: World Wide Web
13   single: URL
14   pair: URL; parsing
15   pair: relative; URL
16
n15+.. note::
16+   The :mod:`urlparse` module is renamed to :mod:`urllib.parse` in Python 3.0.
17+   The :term:`2to3` tool will automatically adapt imports when converting
18+   your sources to 3.0.
19+ 
20+ 
17This module defines a standard interface to break Uniform Resource Locator (URL)
18strings up in components (addressing scheme, network location, path etc.), to
19combine the components back into a URL string, and to convert a "relative URL"
20to an absolute URL given a "base URL."
21
22The module has been designed to match the Internet RFC on Relative Uniform
23Resource Locators (and discovered a bug in an earlier draft!). It supports the
24following URL schemes: ``file``, ``ftp``, ``gopher``, ``hdl``, ``http``,
35.. function:: urlparse(urlstring[, default_scheme[, allow_fragments]])
36
37   Parse a URL into six components, returning a 6-tuple.  This corresponds to the
38   general structure of a URL: ``scheme://netloc/path;parameters?query#fragment``.
39   Each tuple item is a string, possibly empty. The components are not broken up in
40   smaller parts (for example, the network location is a single string), and %
41   escapes are not expanded. The delimiters as shown above are not part of the
42   result, except for a leading slash in the *path* component, which is retained if
n43-   present.  For example::
n47+   present.  For example:
44
45      >>> from urlparse import urlparse
46      >>> o = urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
n47-      >>> o
n51+      >>> o   # doctest: +NORMALIZE_WHITESPACE
48-      ('http', 'www.cwi.nl:80', '/%7Eguido/Python.html', '', '', '')
52+      ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
53+                  params='', query='', fragment='')
49      >>> o.scheme
50      'http'
51      >>> o.port
52      80
53      >>> o.geturl()
54      'http://www.cwi.nl:80/%7Eguido/Python.html'
55
56   If the *default_scheme* argument is specified, it gives the default addressing
85   | :attr:`password` |       | Password                 | :const:`None`        |
86   +------------------+-------+--------------------------+----------------------+
87   | :attr:`hostname` |       | Host name (lower case)   | :const:`None`        |
88   +------------------+-------+--------------------------+----------------------+
89   | :attr:`port`     |       | Port number as integer,  | :const:`None`        |
90   |                  |       | if present               |                      |
91   +------------------+-------+--------------------------+----------------------+
92
n93-   See section :ref:`urlparse-result-object`, "Results of :func:`urlparse` and
n98+   See section :ref:`urlparse-result-object` for more information on the result
94-   :func:`urlsplit`," for more information on the result object.
99+   object.
95
96   .. versionchanged:: 2.5
97      Added attributes to return value.
98
n104+.. function:: parse_qs(qs[, keep_blank_values[, strict_parsing]])
105+ 
106+   Parse a query string given as a string argument (data of type
107+   :mimetype:`application/x-www-form-urlencoded`).  Data are returned as a
108+   dictionary.  The dictionary keys are the unique query variable names and the
109+   values are lists of values for each name.
110+ 
111+   The optional argument *keep_blank_values* is a flag indicating whether blank
112+   values in URL encoded queries should be treated as blank strings.   A true value
113+   indicates that blanks should be retained as  blank strings.  The default false
114+   value indicates that blank values are to be ignored and treated as if they were
115+   not included.
116+ 
117+   The optional argument *strict_parsing* is a flag indicating what to do with
118+   parsing errors.  If false (the default), errors are silently ignored.  If true,
119+   errors raise a :exc:`ValueError` exception.
120+ 
121+   Use the :func:`urllib.urlencode` function to convert such dictionaries into
122+   query strings.
123+ 
124+ 
125+.. function:: parse_qsl(qs[, keep_blank_values[, strict_parsing]])
126+ 
127+   Parse a query string given as a string argument (data of type
128+   :mimetype:`application/x-www-form-urlencoded`).  Data are returned as a list of
129+   name, value pairs.
130+ 
131+   The optional argument *keep_blank_values* is a flag indicating whether blank
132+   values in URL encoded queries should be treated as blank strings.   A true value
133+   indicates that blanks should be retained as  blank strings.  The default false
134+   value indicates that blank values are to be ignored and treated as if they were
135+   not included.
136+ 
137+   The optional argument *strict_parsing* is a flag indicating what to do with
138+   parsing errors.  If false (the default), errors are silently ignored.  If true,
139+   errors raise a :exc:`ValueError` exception.
140+ 
141+   Use the :func:`urllib.urlencode` function to convert such lists of pairs into
142+   query strings.
99
100.. function:: urlunparse(parts)
101
102   Construct a URL from a tuple as returned by ``urlparse()``. The *parts* argument
n103-   be any six-item iterable. This may result in a slightly different, but
n147+   can be any six-item iterable. This may result in a slightly different, but
104   equivalent URL, if the URL that was parsed originally had unnecessary delimiters
105   (for example, a ? with an empty query; the RFC states that these are
106   equivalent).
107
108
109.. function:: urlsplit(urlstring[, default_scheme[, allow_fragments]])
110
111   This is similar to :func:`urlparse`, but does not split the params from the URL.
136   | :attr:`password` |       | Password                | :const:`None`        |
137   +------------------+-------+-------------------------+----------------------+
138   | :attr:`hostname` |       | Host name (lower case)  | :const:`None`        |
139   +------------------+-------+-------------------------+----------------------+
140   | :attr:`port`     |       | Port number as integer, | :const:`None`        |
141   |                  |       | if present              |                      |
142   +------------------+-------+-------------------------+----------------------+
143
n144-   See section :ref:`urlparse-result-object`, "Results of :func:`urlparse` and
n188+   See section :ref:`urlparse-result-object` for more information on the result
145-   :func:`urlsplit`," for more information on the result object.
189+   object.
146
147   .. versionadded:: 2.2
148
149   .. versionchanged:: 2.5
150      Added attributes to return value.
151
152
153.. function:: urlunsplit(parts)
154
155   Combine the elements of a tuple as returned by :func:`urlsplit` into a complete
n156-   URL as a string. The *parts* argument be any five-item iterable. This may result
n200+   URL as a string. The *parts* argument can be any five-item iterable. This may
157-   in a slightly different, but equivalent URL, if the URL that was parsed
201+   result in a slightly different, but equivalent URL, if the URL that was parsed
158   originally had unnecessary delimiters (for example, a ? with an empty query; the
159   RFC states that these are equivalent).
160
161   .. versionadded:: 2.2
162
163
164.. function:: urljoin(base, url[, allow_fragments])
165
n166-   Construct a full ("absolute") URL by combining a "base URL" (*base*) with a
n210+   Construct a full ("absolute") URL by combining a "base URL" (*base*) with
167-   "relative URL" (*url*).  Informally, this uses components of the base URL, in
211+   another URL (*url*).  Informally, this uses components of the base URL, in
168   particular the addressing scheme, the network location and (part of) the path,
n169-   to provide missing components in the relative URL.  For example::
n213+   to provide missing components in the relative URL.  For example:
170
171      >>> from urlparse import urljoin
172      >>> urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html')
173      'http://www.cwi.nl/%7Eguido/FAQ.html'
174
175   The *allow_fragments* argument has the same meaning and default as for
176   :func:`urlparse`.
n221+ 
222+   .. note::
223+ 
224+      If *url* is an absolute URL (that is, starting with ``//`` or ``scheme://``),
225+      the *url*'s host name and/or scheme will be present in the result.  For example:
226+ 
227+   .. doctest::
228+ 
229+      >>> urljoin('http://www.cwi.nl/%7Eguido/Python.html',
230+      ...         '//www.python.org/%7Eguido')
231+      'http://www.python.org/%7Eguido'
232+ 
233+   If you do not want that behavior, preprocess the *url* with :func:`urlsplit` and
234+   :func:`urlunsplit`, removing possible *scheme* and *netloc* parts.
177
178
179.. function:: urldefrag(url)
180
181   If *url* contains a fragment identifier, returns a modified version of *url*
182   with no fragment identifier, and the fragment identifier as a separate string.
183   If there is no fragment identifier in *url*, returns *url* unmodified and an
184   empty string.
212.. method:: ParseResult.geturl()
213
214   Return the re-combined version of the original URL as a string. This may differ
215   from the original URL in that the scheme will always be normalized to lower case
216   and empty components may be dropped. Specifically, empty parameters, queries,
217   and fragment identifiers will be removed.
218
219   The result of this method is a fixpoint if passed back through the original
n220-   parsing function::
n278+   parsing function:
221
222      >>> import urlparse
223      >>> url = 'HTTP://www.Python.org/doc/#'
224
225      >>> r1 = urlparse.urlsplit(url)
226      >>> r1.geturl()
227      'http://www.Python.org/doc/'
228
229      >>> r2 = urlparse.urlsplit(r1.geturl())
230      >>> r2.geturl()
231      'http://www.Python.org/doc/'
232
233   .. versionadded:: 2.5
234
t235-The following classes provide the implementations of the parse results::
t293+The following classes provide the implementations of the parse results:
236
237
238.. class:: BaseResult
239
240   Base class for the concrete result classes.  This provides most of the attribute
241   definitions.  It does not provide a :meth:`geturl` method.  It is derived from
242   :class:`tuple`, but does not override the :meth:`__init__` or :meth:`__new__`
243   methods.
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op