Appending query parameters to a URL is a very common need. However, there's nothing in urllib.parse (and older urlparse) that caters for that need.
Therefore, I propose adding the following to 2.7 and 3.1 in the respective libs:
def add_query_params(url, **params):
"""
Adds additional query parameters to the given url, preserving original
parameters.
Usage:
>>> add_query_params('
http://foo.com', a='b')
'
http://foo.com?a=b'
>>> add_query_params('
http://foo.com?a=b', b='c', d='q')
'
http://foo.com?a=b&b=c&d=q'
The real implementation should be more strict, e.g. raise on the
following:
>>> add_query_params('
http://foo.com?a=b', a='b')
'
http://foo.com?a=b&a=b'
"""
if not params:
return url
encoded = urllib.urlencode(params)
url = urlparse.urlparse(url)
return urlparse.urlunparse((url.scheme, url.netloc, url.path, url.params,
(encoded if not url.query else url.query + '&' + encoded),
url.fragment))