Add params to given URL in Python


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?

Asked By: z4y4ts



Yes: use urllib.

From the examples in the documentation:

>>> import urllib
>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
>>> f = urllib.urlopen("" % params)
>>> print f.geturl() # Prints the final URL with parameters.
>>> print # Prints the contents
Answered By: unwind

Use the various urlparse functions to tear apart the existing URL, urllib.urlencode() on the combined dictionary, then urlparse.urlunparse() to put it all back together again.

Or just take the result of urllib.urlencode() and concatenate it to the URL appropriately.

You want to use URL encoding if the strings can have arbitrary data (for example, characters such as ampersands, slashes, etc. will need to be encoded).

Check out urllib.urlencode:

>>> import urllib
>>> urllib.urlencode({'lang':'en','tag':'python'})

In python3:

from urllib import parse
Answered By: Mike Mueller

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.

Answered By: Łukasz

In python 2.5

import cgi
import urllib
import urlparse

def add_url_param(url, **params):
    parts = list(urlparse.urlsplit(url))
    d = dict(cgi.parse_qsl(parts[n])) # use cgi.parse_qs for list values
    return urlparse.urlunsplit(parts)

url = ""
add_url_param(url, lang='en') == ""
Answered By: Daniel Patru

I liked Łukasz version, but since urllib and urllparse functions are somewhat awkward to use in this case, I think it’s more straightforward to do something like this:

params = urllib.urlencode(params)

if urlparse.urlparse(url)[4]:
    print url + '&' + params
    print url + '?' + params
Answered By: Facundo Olano

Here is how I implemented it.

import urllib

params = urllib.urlencode({'lang':'en','tag':'python'})
url = ''
if request.GET:
   url = request.url + '&' + params
   url = request.url + '?' + params    

Worked like a charm. However, I would have liked a more cleaner way to implement this.

Another way of implementing the above is put it in a method.

import urllib

def add_url_param(request, **params):
   new_url = ''
   _params = dict(**params)
   _params = urllib.urlencode(_params)

   if _params:
      if request.GET:
         new_url = request.url + '&' + _params
         new_url = request.url + '?' + _params
      new_url = request.url

   return new_ur
Answered By: Monty

Yet another answer:

def addGetParameters(url, newParams):
    (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(url)
    queryList = urlparse.parse_qsl(query, keep_blank_values=True)
    for key in newParams:
        queryList.append((key, newParams[key]))
    return urlparse.urlunparse((scheme, netloc, path, params, urllib.urlencode(queryList), fragment))
Answered By: Timmmm

You can also use the furl module

>>> from furl import furl
>>> print furl('').add({'lang':'en','tag':'python'}).url
Answered By: surfeurX


I’ve been not satisfied with all the solutions on this page (come on, where is our favorite copy-paste thing?) so I wrote my own based on answers here. It tries to be complete and more Pythonic. I’ve added a handler for dict and bool values in arguments to be more consumer-side (JS) friendly, but they are yet optional, you can drop them.

How it works

Test 1: Adding new arguments, handling Arrays and Bool values:

url = ''
new_params = {'answers': False, 'data': ['some','values']}

add_url_params(url, new_params) == 

Test 2: Rewriting existing args, handling DICT values:

url = ''
new_params = {'question': {'__X__':'__Y__'}}

add_url_params(url, new_params) == 

Talk is cheap. Show me the code.

Code itself. I’ve tried to describe it in details:

from json import dumps

    from urllib import urlencode, unquote
    from urlparse import urlparse, parse_qsl, ParseResult
except ImportError:
    # Python 3 fallback
    from urllib.parse import (
        urlencode, unquote, urlparse, parse_qsl, ParseResult

def add_url_params(url, params):
    """ Add GET params to provided URL being aware of existing.

    :param url: string of target URL
    :param params: dict containing requested params to be added
    :return: string with updated URL

    >> url = ''
    >> new_params = {'answers': False, 'data': ['some','values']}
    >> add_url_params(url, new_params)
    # Unquoting URL first so we don't loose existing args
    url = unquote(url)
    # Extracting url info
    parsed_url = urlparse(url)
    # Extracting URL arguments from parsed URL
    get_args = parsed_url.query
    # Converting URL arguments to dict
    parsed_get_args = dict(parse_qsl(get_args))
    # Merging URL arguments dict with new params

    # Bool and Dict values should be converted to json-friendly values
    # you may throw this part away if you don't like it :)
        {k: dumps(v) for k, v in parsed_get_args.items()
         if isinstance(v, (bool, dict))}

    # Converting URL argument to proper query string
    encoded_get_args = urlencode(parsed_get_args, doseq=True)
    # Creating new parsed result object based on provided with new
    # URL arguments. Same thing happens inside of urlparse.
    new_url = ParseResult(
        parsed_url.scheme, parsed_url.netloc, parsed_url.path,
        parsed_url.params, encoded_get_args, parsed_url.fragment

    return new_url

Please be aware that there may be some issues, if you’ll find one please let me know and we will make this thing better

Answered By: Sapphire64

Based on this answer, one-liner for simple cases (Python 3 code):

from urllib.parse import urlparse, urlencode

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

url += ('&' if urlparse(url).query else '?') + urlencode(params)


url += ('&', '?')[urlparse(url).query == ''] + urlencode(params)
Answered By: Mikhail Gerasimov

If you are using the requests lib:

import requests
params = {'tag': 'python'}
requests.get(url, params=params)
Answered By: Christophe Roussy

Outsource it to the battle tested requests library.

This is how I will do it:

from requests.models import PreparedRequest
url = ''
params = {'lang':'en','tag':'python'}
req = PreparedRequest()
req.prepare_url(url, params)
Answered By: Varun

I find this more elegant than the two top answers:

from urllib.parse import urlencode, urlparse, parse_qs

def merge_url_query_params(url: str, additional_params: dict) -> str:
    url_components = urlparse(url)
    original_params = parse_qs(url_components.query)
    # Before Python 3.5 you could update original_params with 
    # additional_params, but here all the variables are immutable.
    merged_params = {**original_params, **additional_params}
    updated_query = urlencode(merged_params, doseq=True)
    # _replace() is how you can create a new NamedTuple with a changed field
    return url_components._replace(query=updated_query).geturl()

assert merge_url_query_params(
) == ''

The most important things I dislike in the top answers (they are nevertheless good):

  • Łukasz: having to remember the index at which the query is in the URL components
  • Sapphire64: the very verbose way of creating the updated ParseResult

What’s bad about my response is the magically looking dict merge using unpacking, but I prefer that to updating an already existing dictionary because of my prejudice against mutability.

Answered By: butla

python3, self explanatory I guess

from urllib.parse import urlparse, urlencode, parse_qsl

url = ''

parsed = urlparse(url)
current_params = dict(parse_qsl(parsed.query))
new_params = {'location': 'United States'}
merged_params = urlencode({**current_params, **new_params})
parsed = parsed._replace(query=merged_params)

Answered By: revy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.