Fix Python – Add params to given URL in Python


Asked By – z4y4ts

Suppose I was given a URL.
It might already have GET parameters (e.g. or it might not (e.g.

And now I need to add some parameters to it like {'lang':'en','tag':'python'}. In the first case I’m going to have and in the second —

Is there any standard way to do this?

Now we will see solution for issue: Add params to given URL in Python


There are a couple of quirks with the urllib and urlparse modules. Here’s a working example:

    import urlparse
    from urllib import urlencode
except: # For Python 3
    import urllib.parse as urlparse
    from urllib.parse import urlencode

url = ""
params = {'lang':'en','tag':'python'}

url_parts = list(urlparse.urlparse(url))
query = dict(urlparse.parse_qsl(url_parts[4]))

url_parts[4] = urlencode(query)


ParseResult, the result of urlparse(), is read-only and we need to convert it to a list before we can attempt to modify its data.

This question is answered By – Łukasz

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0