245 字
1 分钟
python解码
python 解码
request 请求
原理:
-
字符串在Python内部的表示是unicode编码,需要unicode编码作为中间件
- eg、response 请求后得到的结果的编码,运行到python程序中首先是
unicode编码,先用response.text.encode去加载原本再网页中的编码,然后再将这个网页里使用的编码进行decode(‘utf8’)就可以正常的显示了
- eg、response 请求后得到的结果的编码,运行到python程序中首先是
-
encode 用于在python程序中,unicode 对其他编码的处理,将python程序中的unicode编码encode得到常见的编码,想要输出的话还是要转成utf8,这就要用到decode函数了
-
decode函数是用于 常见编码转换成unicode编码的一种方式
通用解码:
response.text.encode(response.encoding).decode('utf-8')# response.encoding为原来的编码格式,encode后编码为原来的格式,decode后解码为'utf-8'
response.encoding = 'utf8' # 将encoding直接转换成utf8response.content.decode('utf8') # 二进制内容转换成utf8
文件读写操作 codecs.open
import codecswith codecs.open('....txt', 'w', 'utf-8') as f: f.write(...)