Python URL Encoding: Quick Guide

In Python, you can use the quote() function from the urllib library to encode URLs. Here’s an example:

import urllib.parse

url = "https://www.example.com/?name=张三&age=20"
encoded_url = urllib.parse.quote(url)
print(encoded_url)

This can encode Chinese characters and special characters in the URL, resulting in:

https%3A%2F%2Fwww.example.com%2F%3Fname%3D%E5%BC%A0%E4%B8%89%26age%3D20
bannerAds