Using Urlencode in Python

Using Urlencode in Python

Fariba Laiq Apr-14, 2022 Jan-21, 2022 Python Python URL
  1. URL Encode a Dictionary Using urlencode() in Python
  2. URL Encode a Multi-Valued Dictionary Using urlencode() in Python
  3. Use the urlencode() Function in Python 3
  4. Use the urllib.parse.quote_plus() Function to Quote and Encode URL in Python 3
  5. Use the urllib.parse.quote() Function to Quote and Encode URL in Python 3

URL stands for uniform resource locator. URL is simply the address of a resource on the web through the URL the browser requests for the webpage to load.

URL encoding is the process of converting characters into a special format using limited US-ASCII, which is universally accepted by all browsers and web servers.

If a URL contains characters other than characters defined ASCII set, these characters will be converted into a valid ASCII format. It is also known as percent-encoding because we see percent symbols frequently in URLs.

Whenever we call a web API or submit an HTTP form data, we use an encoded URL to encode the query string. In Python, we can URL encode a query string using the urlib.parse module, which further contains a function urlencode() for encoding the query string in URL. The query string is simply a string of key-value pairs.

URL Encode a Dictionary Using urlencode() in Python

If we want to URL encode a string using the urlencode() function, we cannot do it directly because it does not accept a string as an argument. We can pass the argument in the form of a dictionary. Dictionary is a data type in Python that stores the data in a key-value pair.

For example, we have a query string like this:

queryString = 'name=' + 'Jhon' + '&' + 'age=' + '18'

First we have to write it in a dictionary format like this:

queryString = { 'name' : 'Jhon', 'age' : '18'}

In the following code, we have to import the urlib library, and we will pass our query string to the urlencode() function of the parse module of the urlib library. In the output, we will get the required encoded URL.

In the output, the encoded URL will be in the form where spaces will be replaced by + symbol and : symbol is replaced by & symbol because it is the format of the encoded URL of the query string.

Example Code:

#python 3.x
import urllib
queryString = { 'name' : 'Jhon', 'age' : '18'}
urllib.parse.urlencode(queryString)

Output:

#python 3.x
'age=18&name=Jhon'

URL Encode a Multi-Valued Dictionary Using urlencode() in Python

Now let’s say our query string, which is in the form of a dictionary, contains multivalued data like an attribute named colors with values blue, pink, and green, so it’s a multi-values attribute. We can also URL encode such dictionaries in Python.

The urlencode() function takes an optional argument doseq. If we set its value to True, we can avoid special characters in the output.

Example Code:

import urllib
queryString = { 'jeans' : 'Bell Bottom', 'colors' : ['blue', 'pink', 'green']}
urllib.parse.urlencode(queryString, doseq=True)

Output:

'jeans=Bell+Bottom&colors=blue&colors=pink&colors=green'

Use the urlencode() Function in Python 3

We can encode different parameters using the urlencode() function in Python. We can pass the query in the function as key-value pairs of a dictionary or as a list of tuples with two elements to perform encoding with this function.

The characters _, -, ., ~, letters, and digits are not quoted.

In Python 2, this was directly part of the urllib module. The final result was a string.

For example:

import urllib
query = urllib.urlencode({'www.delftstack.com': 20,  'string': 10})
print (query)

Output:

www.delftstack.com=20&string=10

The above code works with Python 2.

In Python 3, the urllib was split into different submodules. The urlencode() function was made a part of the urllib.parse module.

We can use this function by importing the method from this library.

For example:

import urllib.parse
query = urllib.parse.urlencode({'www.delftstack.com': 20,  'string': 10})
print(query)

Output:

www.delftstack.com=20&string=10

The urlencode() function uses the quote_plus() method internally to perform encoding. We can change this using the quote_via parameters.

The urlencode function has two important parameters, encoding and safe. We can specify the ASCII characters safe from the quoting with the safe parameter.

The default value of this parameter is the \ character. The encoding parameter accepts the encoding type to deal with non-ASCII characters.

Use the urllib.parse.quote_plus() Function to Quote and Encode URL in Python 3

We can use the urllib.parse.quote_plus() function replaces the special characters and spaces in a URL with + signs in a string. By default, this function is used in the urlencode() function.

See the code below.

import urllib.parse
query = urllib.parse.quote_plus('delftstack$urllib')
print(query)

Output:

delftstack%24urllib

We can access this function in Python 2 directly under the urllib package.

Use the urllib.parse.quote() Function to Quote and Encode URL in Python 3

The urllib.parse.quote() function accepts a string and replaces the special characters with the %xx. It also accepts the parameters mentioned in the previous functions as well.

For example:

import urllib.parse
query = urllib.parse.quote('delftstack$urllib')
print(query)

Output:

delftstack%24urllib

We can use this function internally with the urlencode() function, bypassing the urllib.parse.quote function as the value of the quote_via parameter.

This function can also be directly accessed in the urllib package in Python 2.

Author: Fariba Laiq
Fariba Laiq avatar Fariba Laiq avatar

I am Fariba Laiq from Pakistan. An android app developer, technical content writer, and coding instructor. Writing has always been one of my passions. I love to learn, implement and convey my knowledge to others.

LinkedIn

Related Article - Python URL

  • Extract Domain From URL in Python
  • Python Url Decode
  • Get Data From a URL in Python