python中http的⼀些编码转换http的数据需要2种编码解码。
1. url中的特殊字符转换,⽐如”,‘,:,//等
python3中通过urllib.parse.quote(..)和urllib.parse.unquote(..)来编码解码。
如:
import urllib.parse
url = "blog.csdn/muzizongheng"
en = urllib.parse.quote(url)
print(en)
de = urllib.parse.unquote(en)
在线url网址编码解码print(de)
en = "http%3A%2F%2Fblog.csdn%2Fmuzizongheng"
de = urllib.parse.unquote(en)
print(de)
输出:
http%3A//blog.csdn/muzizongheng
blog.csdn/muzizongheng
blog.csdn/muzizongheng
2. http的⼀些Key&Value的组装
python3中通过urllib.parse.urlencode(..)来编码
如:
import urllib.parse
data = {
'key1':"value1",
'key2':"value2"
}
print(urllib.parse.urlencode(data))
输出:
key1=value1&key2=value2
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论