furl is a small Python library that makes parsing and manipulating URLs easy.
Python's standard
urllib and
urlparse modules
provide a number of URL related functions, but using these functions to
perform common URL operations proves tedious. Furl makes parsing and
manipulating URLs easy.
Furl is well tested, Unlicensed in the public
domain, and supports Python 3 and PyPy3.
👥 Furl is looking for a lead contributor and maintainer. Would you love
to lead furl, and making working with URLs a joy for everyone in Python?
Please reach out and let me know! 🙌
Code time: Paths and query arguments are easy. Really easy.
>>> from furl import furl
>>> f = furl('http://www.google.com/?one=1&two=2')
>>> f /= 'path'
>>> del f.args['one']
>>> f.args['three'] = '3'
>>> f.url
'http://www.google.com/path?two=2&three=3'
scheme is the scheme string (all lowercase) or None. None means no
scheme. An empty string means a protocol relative URL, like
//www.google.com.
username is the username string for authentication.
password is the password string for authentication with username.
host is the domain name, IPv4, or IPv6 address as a string. Domain names
are all lowercase.
port is an integer or None. A value of None means no port specified and
the default port for the given scheme should be inferred, if possible
(e.g. port 80 for the scheme http).
path is a Path object comprised of path segments.
query is a Query object comprised of key:value query arguments.
fragment is a Fragment object comprised of a Path object and Query object
separated by an optional ? separator.
Scheme, Username, Password, Host, Port, Network Location, and Origin
scheme, username, password, and host are strings or
None. port is an integer or None.
URL paths in furl are Path objects that have segments, a list of zero or
more path segments that can be manipulated directly. Path segments in
segments are percent-decoded and all interaction with segments should
take place with percent-decoded strings.
A path that starts with / is considered absolute, and a Path can be absolute
or not as specified (or set) by the boolean attribute isabsolute. URL Paths
have a special restriction: they must be absolute if a netloc (username,
password, host, and/or port) is present. This restriction exists because a URL
path must start with / to separate itself from the netloc, if
present. Fragment Paths have no such limitation and isabsolute and can be
True or False without restriction.
Here's a URL Path example that illustrates how isabsolute becomes True and
read-only in the presence of a netloc.
>>> f = furl('/url/path')
>>> f.path.isabsolute
True
>>> f.path.isabsolute = False
>>> f.url
'url/path'
>>> f.host = 'blaps.ru'
>>> f.url
'blaps.ru/url/path'
>>> f.path.isabsolute
True
>>> f.path.isabsolute = False
Traceback (most recent call last):
...
AttributeError: Path.isabsolute is True and read-only for URLs with a netloc (a username, password, host, and/or port). URL paths must be absolute if a netloc exists.
>>> f.url
'blaps.ru/url/path'
Conversely, the isabsolute attribute of Fragment Paths isn't bound by the
same read-only restriction. URL fragments are always prefixed by a # character
and don't need to be separated from the netloc.
A path that ends with / is considered a directory, and otherwise considered a
file. The Path attribute isdir returns True if the path is a directory,
False otherwise. Conversely, the attribute isfile returns True if the path
is a file, False otherwise.
>>> f = furl('http://www.google.com/a/directory/')
>>> f.path.isdir
True
>>> f.path.isfile
False
>>> f = furl('http://www.google.com/a/file')
>>> f.path.isdir
False
>>> f.path.isfile
True
A path can be normalized with normalize(), and normalize() returns the
Path object for method chaining.
>>> f = furl('http://www.google.com////a/./b/lolsup/../c/')
>>> f.path.normalize()
>>> f.url
'http://www.google.com/a/b/c/'
Path segments can also be appended with the slash operator, like with
pathlib.Path.
>>> from __future__ import division # For Python 2.x.
>>>
>>> f = furl('path')
>>> f.path /= 'with'
>>> f.path = f.path / 'more' / 'path segments/'
>>> f.url
'/path/with/more/path%20segments/'
For a dictionary representation of a path, use asdict().
URL queries in furl are Query objects that have params, a one dimensional
ordered multivalue dictionary of
query keys and values. Query keys and values in params are percent-decoded
and all interaction with params should take place with percent-decoded
strings.
encode(delimiter='&', quote_plus=True, dont_quote='') can be used to encode
query strings with delimiters like ;, encode spaces as + instead of %20
(i.e. application/x-www-form-urlencoded encoded), or avoid percent-encoding
valid query characters entirely (valid query characters are
/?:@-._~!$&'()*+,;=).
dont_quote accepts True, False, or a string of valid query characters to
not percent-enode. If True, all valid query characters /?:@-._~!$&'()*+,;=
aren't percent-encoded.
Creating hash-bang fragments with furl illustrates the use of Fragment's boolean
attribute separator. When separator is False, the ? that separates
path and query isn't included.
For quick, single-line URL manipulation, the add(), set(), and
remove() methods of furl objects manipulate various URL components and
return the furl object for method chaining.
remove() removes items from a furl object with the optional arguments
args: Shortcut for query_params.
path: A list of path segments to remove from the end of the existing path
segments list, or a path string to remove from the end of the existing
path string, or True to remove the entire path portion of the URL.
query: A list of query keys to remove from the query, if they exist, or
True to remove the entire query portion of the URL.
query_params: A list of query keys to remove from the query, if they
exist.
fragment: If True, remove the entire fragment portion of the URL.
fragment_path: A list of path segments to remove from the end of the
fragment's path segments, or a path string to remove from the end of the
fragment's path string, or True to remove the entire fragment path.
fragment_args: A list of query keys to remove from the fragment's query,
if they exist.
username: If True, remove the username, if it exists.
password: If True, remove the password, if it exists.
Like pathlib.Path,
path segments can be appended to a furl object's Path with the slash operator.
>>> from __future__ import division # For Python 2.x.
>>> f = furl('http://www.google.com/path?example=arg#frag')
>>> f /= 'add'
>>> f = f / 'seg ments/'
>>> f.url
'http://www.google.com/path/add/seg%20ments/?example=arg#frag'
tostr(query_delimiter='&', query_quote_plus=True, query_dont_quote='')
creates and returns a URL string. query_delimiter, query_quote_plus, and
query_dont_quote are passed unmodified to Query.encode() as delimiter,
quote_plus, and dont_quote respectively.
copy() creates and returns a new furl object with an identical URL.
>>> f = furl('http://www.google.com')
>>> f.copy().set(path='/new/path').url
'http://www.google.com/new/path'
>>> f.url
'http://www.google.com'
join() joins the furl object's URL with the provided relative or absolute
URL and returns the furl object for method chaining. join()'s action is the
same as navigating to the provided URL from the current URL in a web browser.