Created
March 21, 2013 18:29
-
-
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)?
This file contains hidden or 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 | |
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