1>>> from urllib.parse import urljoin
2>>> urljoin('/media/path/', 'js/foo.js')
3'/media/path/js/foo.js'
4
1>>> urllib.parse.urlparse("http://example.com/pa/th;param1=foo;param2=bar?name=val#frag")
2ParseResult(scheme='http', netloc='example.com', path='/pa/th', params='param1=foo;param2=bar', query='name=val', fragment='frag')
3
1# The urlsplit() function is an alternative to urlparse().
2# It behaves a little different,
3# because it does not split the parameters from the URL.
4# This is useful for URLs following RFC 2396,
5# which supports parameters for each segment of the path.
6from urlparse import urlsplit
7parsed = urlsplit('http://user:pass@NetLoc:80/path;parameters/path2;parameters2?query=argument#fragment')
8print parsed
9print 'scheme :', parsed.scheme
10print 'netloc :', parsed.netloc
11print 'path :', parsed.path
12print 'query :', parsed.query
13print 'fragment:', parsed.fragment
14print 'username:', parsed.username
15print 'password:', parsed.password
16print 'hostname:', parsed.hostname, '(netloc in lower case)'
17print 'port :', parsed.port
1Parse a URL into 5 components:
2<scheme>://<netloc>/<path>?<query>#<fragment>
3Return a 5-tuple: (scheme, netloc, path, query, fragment).
4Note that we don't break the components up in smaller bits
5(e.g. netloc is a single string) and we don't expand % escapes.