30

I need to add custom parameters to an URL query string using Python

Example: This is the URL that the browser is fetching (GET):

/scr.cgi?q=1&ln=0

then some python commands are executed, and as a result I need to set following URL in the browser:

/scr.cgi?q=1&ln=0&SOMESTRING=1

Is there some standard approach?

Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
J.Olufsen
  • 13,415
  • 44
  • 120
  • 185

4 Answers4

72

You can use urlsplit() and urlunsplit() to break apart and rebuild a URL, then use urlencode() on the parsed query string:

from urllib import urlencode
from urlparse import parse_qs, urlsplit, urlunsplit

def set_query_parameter(url, param_name, param_value):
    """Given a URL, set or replace a query parameter and return the
    modified URL.

    >>> set_query_parameter('http://example.com?foo=bar&biz=baz', 'foo', 'stuff')
    'http://example.com?foo=stuff&biz=baz'

    """
    scheme, netloc, path, query_string, fragment = urlsplit(url)
    query_params = parse_qs(query_string)

    query_params[param_name] = [param_value]
    new_query_string = urlencode(query_params, doseq=True)

    return urlunsplit((scheme, netloc, path, new_query_string, fragment))

Use it as follows:

>>> set_query_parameter("/scr.cgi?q=1&ln=0", "SOMESTRING", 1)
'/scr.cgi?q=1&ln=0&SOMESTRING=1'
mattbasta
  • 13,492
  • 9
  • 47
  • 68
Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
  • 2
    Although this is several years old, I still feel the need to point this out since this question shows up on the front page of Google searches: If you care about order of parameters (which I assume you do, because of the `doseq=True` part), you should be using `parse_qsl()`, rather than `parse_qs()` -- the former returns a list of tuples, whereas the latter returns a dict, which is **not** guaranteed to preserve order when iterated over. Then, adding a param can be done via `query_params.append((param_name, param_value))`. – mhouglum Sep 29 '17 at 17:19
9

Use urlsplit() to extract the query string, parse_qsl() to parse it (or parse_qs() if you don't care about argument order), add the new argument, urlencode() to turn it back into a query string, urlunsplit() to fuse it back into a single URL, then redirect the client.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

You can use python's url manipulation library furl.

import furl
f =  furl.furl("/scr.cgi?q=1&ln=0")  
f.args['SOMESTRING'] = 1
print(f.url)
Mayank Jaiswal
  • 12,338
  • 7
  • 39
  • 41
0
import urllib
url = "/scr.cgi?q=1&ln=0"
param = urllib.urlencode({'SOME&STRING':1})
url = url.endswith('&') and (url + param) or (url + '&' + param)

the docs

khachik
  • 28,112
  • 9
  • 59
  • 94