Thursday, January 6, 2011
A python function to add/update querystring arguments on a given url
def add_get_args_to_url(url, arg_dict):
import urllib, urlparse
try:
from urlparse import parse_qs
except ImportError:
from cgi import parse_qs
url_parts = urlparse.urlparse(url)
qs_args = parse_qs(url_parts[4])
qs_args.update(arg_dict)
new_qs = urllib.urlencode(qs_args, True)
return urlparse.urlunparse(list(url_parts[0:4]) + [new_qs] + list(url_parts[5:]))
Subscribe to:
Posts (Atom)