Search code examples
pythonurl

Add params to given URL in Python


Suppose I was given a URL.
It might already have GET parameters (e.g. http://example.com/search?q=question) or it might not (e.g. http://example.com/).

And now I need to add some parameters to it like {'lang':'en','tag':'python'}. In the first case I'm going to have http://example.com/search?q=question&lang=en&tag=python and in the second — http://example.com/search?lang=en&tag=python.

Is there any standard way to do this?


Solution

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

    try:
        import urlparse
        from urllib import urlencode
    except: # For Python 3
        import urllib.parse as urlparse
        from urllib.parse import urlencode
    
    url = "http://stackoverflow.com/search?q=question"
    params = {'lang':'en','tag':'python'}
    
    url_parts = list(urlparse.urlparse(url))
    query = dict(urlparse.parse_qsl(url_parts[4]))
    query.update(params)
    
    url_parts[4] = urlencode(query)
    
    print(urlparse.urlunparse(url_parts))
    

    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.