Movatterモバイル変換


[0]ホーム

URL:


Up one LevelPython Library ReferenceContentsModule IndexIndex

18.17urlparse -- Parse URLs into components

This module defines a standard interface to break Uniform ResourceLocator (URL) strings up in components (addressing scheme, networklocation, path etc.), to combine the components back into a URLstring, and to convert a ``relative URL'' to an absolute URL given a``base URL.''

The module has been designed to match the Internet RFC on RelativeUniform Resource Locators (and discovered a bug in an earlierdraft!). It supports the following URL schemes:file,ftp,gopher,hdl,http,https,imap,mailto,mms,news,nntp,prospero,rsync,rtsp,rtspu,sftp,shttp,sip,sips,snews,svn,svn+ssh,telnet,wais.

New in version 2.5:Support for thesftp andsips schemes.

Theurlparse module defines the following functions:

urlparse(urlstring[, default_scheme[, allow_fragments]])
Parse a URL into six components, returning a 6-tuple. Thiscorresponds to the general structure of a URL:scheme://netloc/path;parameters?query#fragment.Each tuple item is a string, possibly empty.The components are not broken up in smaller parts (for example, the networklocation is a single string), and % escapes are not expanded.The delimiters as shown above are not part of the result,except for a leading slash in thepath component, which isretained if present. For example:

>>> from urlparse import urlparse>>> o = urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')>>> o('http', 'www.cwi.nl:80', '/%7Eguido/Python.html', '', '', '')>>> o.scheme'http'>>> o.port80>>> o.geturl()'http://www.cwi.nl:80/%7Eguido/Python.html'

If thedefault_scheme argument is specified, it gives thedefault addressing scheme, to be used only if the URL does notspecify one. The default value for this argument is the empty string.

If theallow_fragments argument is false, fragment identifiersare not allowed, even if the URL's addressing scheme normally doessupport them. The default value for this argument isTrue.

The return value is actually an instance of a subclass oftuple. This class has the following additional read-onlyconvenience attributes:

AttributeIndexValueValue if not present
scheme0URL scheme specifierempty string
netloc1Network location partempty string
path2Hierarchical pathempty string
params3Parameters for last path elementempty string
query4Query componentempty string
fragment5Fragment identifierempty string
usernameUser nameNone
passwordPasswordNone
hostnameHost name (lower case)None
portPort number as integer, if presentNone

See section 18.17.1, ``Results ofurlparse() andurlsplit(),'' for moreinformation on the result object.

Changed in version 2.5:Added attributes to return value.

urlunparse(parts)
Construct a URL from a tuple as returned byurlparse().Theparts argument can be any six-item iterable.This may result in a slightly different, but equivalent URL, if theURL that was parsed originally had unnecessary delimiters (for example,a ? with an empty query; the RFC states that these are equivalent).

urlsplit(urlstring[, default_scheme[, allow_fragments]])
This is similar tourlparse(), but does not split theparams from the URL. This should generally be used instead ofurlparse() if the more recent URL syntax allowingparameters to be applied to each segment of thepath portion ofthe URL (seeRFC 2396) is wanted. A separate function is needed toseparate the path segments and parameters. This function returns a5-tuple: (addressing scheme, network location, path, query, fragmentidentifier).

The return value is actually an instance of a subclass oftuple. This class has the following additional read-onlyconvenience attributes:

AttributeIndexValueValue if not present
scheme0URL scheme specifierempty string
netloc1Network location partempty string
path2Hierarchical pathempty string
query3Query componentempty string
fragment4Fragment identifierempty string
usernameUser nameNone
passwordPasswordNone
hostnameHost name (lower case)None
portPort number as integer, if presentNone

See section 18.17.1, ``Results ofurlparse() andurlsplit(),'' for moreinformation on the result object.

New in version 2.2.Changed in version 2.5:Added attributes to return value.

urlunsplit(parts)
Combine the elements of a tuple as returned byurlsplit()into a complete URL as a string.Theparts argument can be any five-item iterable.This may result in a slightly different, but equivalent URL, if theURL that was parsed originally had unnecessary delimiters (for example,a ? with an empty query; the RFC states that these are equivalent).New in version 2.2.

urljoin(base, url[, allow_fragments])
Construct a full (``absolute'') URL by combining a ``base URL''(base) with another URL (url). Informally, thisuses components of the base URL, in particular the addressing scheme,the network location and (part of) the path, to provide missingcomponents in the relative URL. For example:

>>> from urlparse import urljoin>>> urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html')'http://www.cwi.nl/%7Eguido/FAQ.html'

Theallow_fragments argument has the same meaning and default asforurlparse().

Note:Ifurl is an absolute URL (that is, starting with// orscheme://, theurl's host name and/or scheme will be present in the result. For example:

>>> urljoin('http://www.cwi.nl/%7Eguido/Python.html',...         '//www.python.org/%7Eguido')'http://www.python.org/%7Eguido'

If you do not want that behavior, preprocesstheurl withurlsplit() andurlunsplit(),removing possiblescheme andnetloc parts.

urldefrag(url)
Ifurl contains a fragment identifier, returns a modifiedversion ofurl with no fragment identifier, and the fragmentidentifier as a separate string. If there is no fragment identifierinurl, returnsurl unmodified and an empty string.

See Also:

RFC 1738,Uniform Resource Locators (URL)
This specifies the formal syntax and semantics of absolute URLs.
RFC 1808,Relative Uniform Resource Locators
This Request For Comments includes the rules for joining an absolute and a relative URL, including a fair number of ``Abnormal Examples'' which govern the treatment of border cases.
RFC 2396,Uniform Resource Identifiers (URI): Generic Syntax
Document describing the generic syntactic requirements for both Uniform Resource Names (URNs) and Uniform Resource Locators (URLs).



Subsections


Up one LevelPython Library ReferenceContentsModule IndexIndex

Release 2.5.2, documentation updated on 21st February, 2008.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2025 Movatter.jp