Use urllib.urlencode() . It takes a dictionary of key-value pairs and converts it into a form suitable for the URL (for example, key1=val1&key2=val2 ).
If you are using Python3, use urllib.parse.urlencode()
If you want to create a URL with duplicate parameters, for example: p=1&p=2&p=3 , you have two options:
>>> import urllib >>> a = (('p',1),('p',2), ('p', 3)) >>> urllib.urlencode(a) 'p=1&p=2&p=3'
or if you want to create a url with duplicate parameters:
>>> urllib.urlencode({'p': [1, 2, 3]}, doseq=True) 'p=1&p=2&p=3'
mipadi Aug 05 '09 at 14:16 2009-08-05 14:16
source share