Python Url Decode

Python Url Decode

  1. Use the urllib.parse.unquote() Function to Decode a URL in Python
  2. Use the urllib.parse.unquote_plus() Function to Decode a URL in Python
  3. Use the requests Module to Decode a URL in Python

URL encoding is essential when dealing with APIs with added path parameters or query strings. The path parameters and query strings need to be properly encoded in the URL to ensure safety.

Moreover, URL encoding also finds its use in preparing data for submission. However, it is difficult for a programmer to understand this encoded data, which is why there is a need for decoding it.

This tutorial demonstrates the different ways available to decode a URL in Python.

When dealing with HTML forms, they use application/x-www-form-urlencoded content type by default for sending parameters. The parameters received from these forms must be decoded first to make them usable in Python.

Use the urllib.parse.unquote() Function to Decode a URL in Python

The urllib.parse.unquote() function is utilized to transparently and efficiently convert the given string from percent-encoded to UTF-8 bytes data while then further converting it to plain text.

The urllib.parse.unquote() function replaces the %x escape sequence with its single character equivalent. This function works when the given object is either a byte or an str object.

To utilize this function in your python code, you first import the urllib library. This package provides several libraries and functions that make it easy to work with URLs in Python.

The following code uses the urllib.parse.unquote() function to decode a URL in Python.

import urllib.parse
url = 'delftstack.com/code=%20HOW%20TO%20Articles'
x = urllib.parse.unquote(url)
print(x)

The above code provides the following Output:

delftstack.com/code= HOW TO Articles

Use the urllib.parse.unquote_plus() Function to Decode a URL in Python

It works similar to the above-mentioned urllib.parse.unquote() function but has a significant difference further explained.

HTML forms usually contain + signs while decoding the values. While the urllib.parse.unquote() function is incapable of decoding the + sign, the urllib.parse.unquote_plus() function decodes the URL containing the + sign by replacing all the + signs with spaces.

This function works only when the given object is an str object.

The following code uses the urllib.parse.unquote_plus() function to decode a URL in Python.

import urllib.parse
url = 'delftstack.com/code=HOW%20TO+Articles'
x = urllib.parse.unquote_plus(url)
print(x)

The above code provides the following output:

delftstack.com/code=HOW TO Articles

Use the requests Module to Decode a URL in Python

Python has an uncomplicated and efficient requests library that sends HTTP requests through Python. The requests library may also come in handy to complete this task, as this library is usually used when dealing with HTML forms in Python.

Much like the urllib.parse.unquote() function, the requests.utils.unquote() function also does not filter the + sign while decoding the URL.

The following code uses the requests module functions to decode a URL in Python.

import requests
url = "delftstack.com/code=%20HOW%20TO%20Articles"
print(requests.utils.unquote(url))

The above code provides the following Output:

delftstack.com/code= HOW TO Articles

Related Article - Python URL

  • Extract Domain From URL in Python
  • Using Urlencode in Python
  • Get Data From a URL in Python