What to do if you want to pass values of parameters intto an URL string? Suppose you need to get the URL like this:
http://127.0.0.1:5000/data?key=xxxx&secret=xxxx
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 = "http://127.0.0.1:5000/data?key="+key+"&secret="+secret
Better to use string parameters:
key = "xxxx"
secret = "xxxx"
url = "http://127.0.0.1:5000/data?key=%s&secret=%s" % (key, secret)
And you can use string format method:
>>> key = "xxxx"
>>> secret = "xxxx"
>>> url = "http://127.0.0.1:5000/data?key={key}&secret={secret}".format(key=key, secret=secret)
>>> print(url)
http://127.0.0.1:5000/data?key=xxxx&secret=xxxx
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 = "http://127.0.0.1:5000/data?{}".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('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)) print(set_query_parameter("/scr.cgi?q=1&ln=0", "SOMESTRING", 1))
These are not parameters.
What are these then? Arguments?
Found this post on Duck Duck Go, while looking for elegant solutions, so I guess others will find it as well.
This Stack Overflow post has some solid approaches as well: https://stackoverflow.com/questions/2506379/add-params-to-given-url-in-python/52373377#52373377
Great
Your string parameter construct was very helpful to me. I had been searching a way to manipulate the end of a static url. Your post allowed me to finally get my code working and it works wonderfully.
Thank you.