Python: Adding parameters to URL using urllib
import urllib.parse
url = 'http://stackoverflow.com/search'
params = {
'lang': 'en',
'tag': 'python tkinter'
}
print(url + '?' + urllib.parse.urlencode(params))
Result:
http://stackoverflow.com/search?lang=en&tag=python+tkinter
Sometimes you have to keep some original chars and then you can use parameter safe
import urllib.parse
url = 'http://stackoverflow.com/search'
params = {
'lang': 'en',
'tag': 'python,tkinter'
}
print(url + '?' + urllib.parse.urlencode(params))
print(url + '?' + urllib.parse.urlencode(params, safe=','))
Result:
http://stackoverflow.com/search?lang=en&tag=python%2Ctkinter
http://stackoverflow.com/search?lang=en&tag=python,tkinter