How to add parameters to the URL string in Python

What to do if you want to pass values of parameters intto an URL string? Suppose you need to get the URL like this:

In Python, how can you add the variables to a URL?

How to construct the URL with parameters

You can concat your string and your variables:

key = "xxxx"
secret = "xxxx"
url = ""+key+"&secret="+secret

Better to use string parameters:

key = "xxxx"
secret = "xxxx"
url = "" % (key, secret)

And you can use string format method:

>>> key = "xxxx"
>>> secret = "xxxx"
>>> url = "{key}&secret={secret}".format(key=key, secret=secret)
>>> print(url)

But you will want to make sure your values are url encoded.

The only characters that are safe to send non-encoded are [0-9a-zA-Z] and $-_.+!*'()

Everything else needs to be encoded.

The safest way is to pass parameters into the URL string do the following:

import urllib

args = {"key": "xxxx", "secret": "yyyy"}
url = "{}".format(urllib.urlencode(args))

For additional information read over page 2 of RFC1738

How to add parameter into the existing URL

If you want to add a parameter into the existing URL

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

from urllib.parse import urlencode, 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('', 'foo', 'stuff')

    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))

print(set_query_parameter("/scr.cgi?q=1&ln=0", "SOMESTRING", 1))

Leave a Reply

Your email address will not be published. Required fields are marked *