urllib中的网页的编码(urlencode)、解码(unquote)

python3中的urllib网页的编码(urlencode)、解码(unquote),以访问百度为例,在接口输入字符的时候为中文,但是计算机需要编码才能识别。

from urllib import request
from urllib import parse     #导入编码  解码包
#百度url     https://www.baidu.com/s?wd=关晓彤      网页输入
#百度url     https://www.baidu.com/s?wd=%E5%85%B3%E6%99%93%E5%BD%A4    实际输出可以辨别的代码
word={"wd":"关晓彤"}
print(parse.urlencode(word))    # urlencode  中文  编码转化为  url识别的字符
#print(type(parse.urlencode(word)))
print(parse.unquote(parse.urlencode(word)))  #  unquote  解码为中文字符

运行结果如下:

wd=%E5%85%B3%E6%99%93%E5%BD%A4
wd=关晓彤

Process finished with exit code 0
原文地址:https://www.cnblogs.com/my-global/p/12441117.html