在Python中,可以使用urllib庫中的quote()函數來進行URL編碼,示例如下:
import urllib.parse
url = "https://www.example.com/?name=張三&age=20"
encoded_url = urllib.parse.quote(url)
print(encoded_url)
這樣就可以將URL中的中文字符和特殊字符進行編碼,輸出結果為:
https%3A%2F%2Fwww.example.com%2F%3Fname%3D%E5%BC%A0%E4%B8%89%26age%3D20