Home  >  Article  >  Backend Development  >  How to use the urllib.parse.quote() function to encode URLs in Python 3.x

How to use the urllib.parse.quote() function to encode URLs in Python 3.x

WBOY
WBOYOriginal
2023-07-31 22:46:562801browse

Use the urllib.parse.quote() function to encode URLs in Python 3.x

In network application development, we often encounter situations where URLs need to be encoded. This is due to the The allowed characters are limited, and the parameters we need to pass may contain special characters. The urllib.parse module in Python provides the quote() function, which can encode illegal characters in URLs into legal URL strings. This article will use code examples to introduce how to use the urllib.parse.quote() function to encode URLs in Python 3.x.

First, we need to import the urllib.parse module, using the following line of code:

from urllib.parse import quote

Next, we can use the quote() function to encode the URL string. The following code example will encode a URL containing special characters and print the encoded result:

url = 'http://www.example.com/?keyword=hello world'
encoded_url = quote(url)
print(encoded_url)

Run the above code and the output will be as follows:

http%3A//www.example.com/%3Fkeyword%3Dhello%20world

As you can see, quote( ) function replaces illegal characters in the URL with corresponding encoded strings.

quote() function can also receive an optional safe parameter to specify characters that do not need to be encoded. In URL encoding, only non-alphanumeric characters are encoded by default. If you want some special characters not to be encoded, you can add them to the safe parameter. For example:

url = 'http://www.example.com/?query=python 3.x'
encoded_url = quote(url, safe='/:')
print(encoded_url)

Run the above code, the output result is as follows:

http://www.example.com/?query=python%203.x

You can see that because we specified the characters '/' and ':' in the safe parameter, these characters are used when encoding. Characters will not be replaced.

In addition, the quote() function also provides two encoding modes. UTF-8 encoding is used by default, and other encoding methods can also be specified. For example:

url = 'http://www.example.com/?query=你好'
encoded_url = quote(url, encoding='gbk')
print(encoded_url)

Run the above code, the output result is as follows:

http%3A%2F%2Fwww.example.com%2F%3Fquery%3D%B2%E2%CA%D4

As you can see, because we use gbk encoding, the encoding result is different from the above example.

To summarize, the urllib.parse.quote() function in Python 3.x can easily encode URLs and convert illegal characters into legal URL strings. Through the introduction and code examples of this article, I believe that readers have mastered how to use this function to encode URLs in Python. In actual development, we can flexibly encode the URL as needed to ensure that the parameters passed will not destroy the format and semantics of the URL.

Reference materials:

  • [Python official documentation - urllib.parse](https://docs.python.org/3/library/urllib.parse.html)

The above is the detailed content of How to use the urllib.parse.quote() function to encode URLs in Python 3.x. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn