1.编码:
from urllib.parse import quote
url_to_encode = "这是一个测试 URL! @#$%^&*() "
encoded_url = quote(url_to_encode, safe='')
print(encoded_url) # 输出: %E8%BF%99%E6%98%AF%E4%B8%80%E4%B8%AA%E6%B5%8B%E8%AF%95%20URL%21%20%40%23%24%25%5E%26*()%20
2.解码:
from urllib.parse import unquote
url_to_decode = "%E8%BF%99%E6%98%AF%E4%B8%80%E4%B8%AA%E6%B5%8B%E8%AF%95%20URL%21%20%40%23%24%25%5E%26*()%20"
decoded_url = unquote(url_to_decode)
print(decoded_url) # 输出: 这是一个测试 URL! @#$%^&*()
3.requests.get和requests.post
requests.get:
Python的requests.get方法在构造请求时会自动对URL参数进行编码,以确保参数能够安全地传输。
requests.post:
当发送数据时,如果数据是字典,requests会自动编码为URL编码格式(application/x-www-form-urlencoded)。如果数据是字符串,则会按原样发送。如果数据是字节,则会直接发送,不会进行编码。