Skip to content

Instantly share code, notes, and snippets.

@itsjohncs
Created June 25, 2012 01:54
Show Gist options
  • Save itsjohncs/2985976 to your computer and use it in GitHub Desktop.
Save itsjohncs/2985976 to your computer and use it in GitHub Desktop.
add_get_params
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