from urlparse import urlparse, urlunparse from urllib import urlencode def parametrize_url(url, **params): """ don't just add the **params because the url itself might contain CGI variables embedded inside the string. """ url_parsed = list(urlparse(url)) encoded = urlencode(params) qs = url_parsed[4] if encoded: if qs: qs += '&'+encoded else: qs = encoded netloc = url_parsed[1] if netloc.find('?')>-1: url_parsed[1] = url_parsed[1][:netloc.find('?')] if qs: qs = netloc[netloc.find('?')+1:]+'&'+qs else: qs = netloc[netloc.find('?')+1:] url_parsed[4] = qs url = urlunparse(url_parsed) return url if __name__=='__main__': def x(u,**k): print parametrize_url(u, **k), "\t", u, k x('http://www.peterbe.com') x('http://www.peterbe.com/') x('http://www.peterbe.com/photos') x('http://www.peterbe.com/photos/') x('http://www.peterbe.com?a=A') x('http://www.peterbe.com/?a=A') x('http://www.peterbe.com/photos?a=A') x('http://www.peterbe.com/photos/?a=A') x('http://www.peterbe.com', a='A') x('http://www.peterbe.com/', a='A') x('http://www.peterbe.com/photos', a='A') x('http://www.peterbe.com/photos/', a='A') x('http://www.peterbe.com?a=A', a='A') x('http://www.peterbe.com/?a=A', a='A') x('http://www.peterbe.com/photos?a=A', a='A') x('http://www.peterbe.com/photos/?a=A', a='A') x('http://www.peterbe.com', z='Z') x('http://www.peterbe.com/', z='Z') x('http://www.peterbe.com/photos', z='Z') x('http://www.peterbe.com/photos/', z='Z') x('http://www.peterbe.com?a=A', z='Z') x('http://www.peterbe.com/?a=A', z='Z') x('http://www.peterbe.com/photos?a=A', z='Z') x('http://www.peterbe.com/photos/?a=A', z='Z') x('http://www.peterbe.com', z='Z', y='Y') x('http://www.peterbe.com/', z='Z', y='Y') x('http://www.peterbe.com/photos', z='Z', y='Y') x('http://www.peterbe.com/photos/', z='Z', y='Y') x('http://www.peterbe.com?a=A', z='Z', y='Y') x('http://www.peterbe.com/?a=A', z='Z', y='Y') x('http://www.peterbe.com/photos?a=A', z='Z', y='Y') x('http://www.peterbe.com/photos/?a=A', z='Z', y='Y')