Created
June 25, 2012 01:54
-
-
Save itsjohncs/2985976 to your computer and use it in GitHub Desktop.
add_get_params
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urlparse | |
def add_get_params(url, params): | |
"Add the given parameters to a URL's query." | |
components = urlparse.urlsplit(url) | |
# Form a list containing each of the get parameters including the new ones. | |
new_query = components.query.split("&") + \ | |
[str(k) + "=" + str(v) for k, v in params.items()] | |
# Join the parameters together ignoring any empty ones | |
new_query = "&".join((i for i in new_query if i)) | |
return urlparse.urlunsplit(urlparse.SplitResult( | |
components.scheme, | |
components.netloc, | |
components.path, | |
new_query, | |
components.fragment | |
)) | |
# example | |
print add_get_params("http://www.google.com/moose?", {"b": "2"}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment