# Python : Core Library : Web ## urllib.parse #### Imports ```python from urllib.parse import ( # URL Encoding & Decoding quote, unquote, # encoded str <-> unencoded str (meant for URL-encoding 'path' part) urlencode, parse_qs, # querystring <-> params dict (meant for URL-encoding 'querystring' part]) urlsplit, urlunsplit, # URL <-> parts tuple # ignore urlparse / urlunparse ) ``` #### API ```python quote( string, safe = '/', encoding = None, errors = None ) # safe='/' means won't quote slashes unquote( string, encoding = 'utf-8', errors = 'replace' ) urlencode( query, doseq = False, safe = '', encoding = None, errors = None, quote_via = quote_plus ) # query -> mapping or sequence of pairs # set doseq=True to support multiple values per key parse_qs( qs, keep_blank_values = False, strict_parsing = False, encoding = 'utf-8', errors = 'replace', max_num_fields = None, separator = '&' ) # -> dict of value-lists urlsplit( urlstring, scheme = '', allow_fragments = True ) -> SplitResult (five-part namedtuple) urlunsplit( five-part-tuple ) -> urlstring SplitResult( scheme, netloc, path, query, fragment ) .username -> str | None .password -> str | None .hostname -> str .port -> int | None # raises ValueError if not int .geturl() -> urlstring # same as `urlunsplit( result )` ._asdict() ._replace( PART="..." ) -> new SplitResult ``` #### Examples ```python urlencode( dict( a=1, b=[ 2,3 ] ) ) -> 'a=1&b=%5B2%2C+3%5D' urlencode( dict( a=1, b=[ 2,3 ] ), doseq=True ) -> 'a=1&b=2&b=3' urlsplit( "http://docs.python.org:80/3/library/urllib.parse.html?highlight=params#url-parsing" ) -> SplitResult( scheme = "http" netloc = "docs.python.org:80" path = "/3/library/urllib.parse.html" query = "highlight=params" fragment = "url-parsing" ) .username -> None .password -> None .hostname -> "docs.python.org" .port -> 80 urlsplit( "foo.com/index.html" ) -> SplitResult( scheme='', netloc='', path='foo.com/index.html', query='', fragment='' ) # must include scheme to force interpretation of "foo.com" as hostname instead of path urlsplit( "http://foo.com/index.html" ) -> SplitResult( scheme='http', netloc='foo.com', path='/index.html', query='', fragment='' ) ```