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.<br><div class="gmail_quote"><br>Therefore, I propose adding the following to 2.7 and 3.1 in the respective libs:<br>
<br>def add_query_params(url, **params):<br> """<br> Adds additional query parameters to the given url, preserving original<br> parameters.<br><br> Usage:<br> >>> add_query_params('<a href="http://foo.com" target="_blank">http://foo.com</a>', a='b')<br>
'<a href="http://foo.com?a=b" target="_blank">http://foo.com?a=b</a>'<br> >>> add_query_params('<a href="http://foo.com?a=b" target="_blank">http://foo.com?a=b</a>', b='c', d='q')<br>
'<a href="http://foo.com?a=b&b=c&d=q" target="_blank">http://foo.com?a=b&b=c&d=q</a>'<br>
<br> The real implementation should be more strict, e.g. raise on the<br> following:<br> >>> add_query_params('<a href="http://foo.com?a=b" target="_blank">http://foo.com?a=b</a>', a='b')<br>
'<a href="http://foo.com?a=b&a=b" target="_blank">http://foo.com?a=b&a=b</a>'<br>
"""<br> if not params:<br> return url<br> encoded = urllib.urlencode(params)<br> url = urlparse.urlparse(url)<br> return urlparse.urlunparse((url.scheme, url.netloc, url.path, url.params,<br>
(encoded if not url.query else url.query + '&' + encoded),<br> url.fragment))<br><br>
</div><br>