Search on blog:

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

urlparse przykład

>>> import urlparse

>>> url = "http://www.example.org/default.html?alpha=32&beta=92&gamma=98"

>>> urlparse.urlsplit(url)
SplitResult(scheme='http', netloc='www.example.org', path='/default.html', query='ct=32&op=92&item=98', fragment='')

>>> urlparse.parse_qs(urlparse.urlsplit(url).query)
{'alpha': ['32'], 'betta': ['92'], 'gamma': ['98']}

>>> dict(urlparse …

« Page: 1 / 1 »