url编码与解码

URL编码格式函数描述:

注: urllib2 在python3中变为 urllib.request
注: urllib.urlencode  在python3中变为 urllib.parser.urlencode

编码:

编码工作使用的urllib.parse.urlencode()函数, 帮我们将key:value这样的键值对转换成"key-value"这样的字符串,

解码:

解码工作可以使用urllib.parse.unquote()方法


一般HTTP请求提交数据, 需要编码成URL编码格式 然后作为URL的一部分, 或者作为参数传到Request对象

示例:

from urllib import parse

data = {"wd": "李颖你吃屁哈哈哈哈"}

encode_data = parse.urlencode(data)
print("编码:"+str(encode_data))


decode_data = parse.unquote(encode_data, 'utf-8')
print("解码:"+str(decode_data))

IPython3终端演示:

 
原文地址:https://www.cnblogs.com/amou/p/9129370.html