How to use urllib.parse.unquote() function to decode URL in Python 3.x

How to use urllib.parse.unquote() function to decode URL in Python 3.x

In Python's urllib library, the urllib.parse module provides a series of utility functions for URL encoding and decoding, among which the urllib.parse.unquote() function can be used to decode URLs. This article describes how to use the urllib.parse.unquote() function to decode URLs, and gives code examples.

URL encoding is often encountered when making network requests or when manipulating URL strings. URL encoding is to encode specific characters in a special format for transmission or storage on the network. And when we need to get the actual content from the URL, we need to decode it.

Python's urllib.parse.unquote() function can decode encoded characters in a URL into the corresponding string. The function is defined as follows:

urllib.parse.unquote(string, encoding='utf-8', errors='replace')

Parameter Description:

  • string: URL string to be decoded.
  • encoding: The encoding used when decoding, the default is UTF-8.
  • errors: The processing method when an error is encountered during decoding, the default is 'replace', that is, the character that cannot be decoded is replaced by '?'.

The following is an example to demonstrate how to use the urllib.parse.unquote() function for decoding:

import urllib.parse

# 需要解码的 URL
encoded_url = 'https://www.example.com/%E6%B5%8B%E8%AF%95%E5%AD%97%E7%AC%A6%E4%B8%B2'

# 对 URL 进行解码
decoded_url = urllib.parse.unquote(encoded_url)

# 打印解码后的 URL
print(decoded_url)

In the above code, we first define a URL that needs to be decoded, which contains an encoded string. Then use the urllib.parse.unquote() function to decode the URL to get the decoded URL. Finally, print the decoded URL to see the decoded result.

Run the above code, the output is as follows:

https://www.example.com/测试字符串

You can see that the encoded string in the decoded URL has been correctly converted back to the corresponding characters.

In addition to decoding the entire URL, the urllib.parse.unquote() function can also decode specific parts of the URL. For example, to decode query string parameters in a URL:

import urllib.parse

# 需要解码的查询字符串参数
encoded_param = 'q%23=%E6%B5%8B%E8%AF%95'

# 对查询字符串参数进行解码
decoded_param = urllib.parse.unquote(encoded_param)

# 打印解码后的查询字符串参数
print(decoded_param)

Run the above code, the output is as follows:

q#=测试

As you can see, the decoded query string parameters have been correctly converted back to the corresponding characters. In actual development, we can choose to decode specific parts of the URL as needed for subsequent processing.

Summary:
This article introduces how to use the urllib.parse.unquote() function to decode URLs in Python 3.x. Use this function to easily decode the encoded string in the URL into corresponding characters for subsequent processing. During development, you can choose to decode the entire URL or part of the content according to actual needs to meet specific business needs. I hope this article will help you learn and use Python's urllib.parse.unquote() function.

The above is the details of how to use the urllib.parse.unquote() function to decode URLs in Python 3.x

Guess you like

Origin blog.csdn.net/lmrylll/article/details/132167416