Skip to content

Instantly share code, notes, and snippets.

@aparrish
Created March 21, 2013 18:29
Show Gist options
  • Save aparrish/5215437 to your computer and use it in GitHub Desktop.
Save aparrish/5215437 to your computer and use it in GitHub Desktop.
add a parameter to the query string of a URL. is this really the easiest way to do this (without resorting to a non-standard library)?
import urlparse
import urllib
def add_param_to_url(url, param, val):
parsed = urlparse.urlsplit(url)._asdict()
query = urlparse.parse_qs(parsed['query'])
query[param] = [val]
parsed['query'] = urllib.urlencode(query, True)
return urlparse.urlunsplit(urlparse.SplitResult(**parsed))
if __name__ == '__main__':
import sys
print add_param_to_url(*sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment