python --- request返回值乱码问题
1、分析
a)《HTTP权威指南》里第16章国际化里提到,如果HTTP响应中Content-Type字段没有指定charset,则默认页面是'ISO-8859-1'编码。一般现在页面编码都直接在html页面中

这处理英文页面当然没有问题,但是中文页面,就会有乱码了!
b)分析requests的源代码发现,content是urllib3读取回来的原始字节码,而text不过是尝试对content通过编码方式解码为unicode,即text返回的是处理过的Unicode型的数据,而使用content返回的是bytes型的原始数据,程序只通过http响应首部获取编码,假如响应中,没有指定charset, 那么直接返回'ISO-8859-1'。
c)本文中返回的html编码为utf-8,所以解码时出现了问题
2、解决办法
a)requests模块中自带该属性,但默认并没有用上,所以我们可以直接执行encoding为正确编码,让response.text正确解码即可
import requests
url = 'http://192.168.127.129/bugfree/index.php/site/login'
data = {
'username': 'admin',
'password': '',
'language': 'zh_cn',
'rememberMe': ''
} header = {
'User-Agent': 'Mozilla/5.0'
} cookie = {
'1_product': 'a6c11f988efefbf5398458aaf673011a504bf08ds%3A1%3A%221%22%3B',
'pageSize': '6f3ba80f2ac7df7b59e81c6cacbe4c041c5a706ds%3A2%3A%2220%22%3B',
'PHPSESSID': 'ku858m8vbmli7hp4inic0pifh7',
'language': 'bece46be16477e1ab82f9d40a53074cb0a54e105s%3A5%3A%22zh_cn%22%3B'
} res = requests.post(url,data,headers=header,cookies=cookie)
res.encoding = res.apparent_encoding
print(res.text)
b)由于content是HTTP相应的原始字节串,所以我们需要直接可以通过使用它。把content按照页面编码方式解码为unicode!
import requests
url = 'http://192.168.127.129/bugfree/index.php/site/login'
data = {
'username': 'admin',
'password': '',
'language': 'zh_cn',
'rememberMe': ''
} header = {
'User-Agent': 'Mozilla/5.0'
} cookie = {
'1_product': 'a6c11f988efefbf5398458aaf673011a504bf08ds%3A1%3A%221%22%3B',
'pageSize': '6f3ba80f2ac7df7b59e81c6cacbe4c041c5a706ds%3A2%3A%2220%22%3B',
'PHPSESSID': 'ku858m8vbmli7hp4inic0pifh7',
'language': 'bece46be16477e1ab82f9d40a53074cb0a54e105s%3A5%3A%22zh_cn%22%3B'
} res = requests.post(url,data,headers=header,cookies=cookie)
print(res.content.decode('utf-8'))
解决前:

解决后:

参考博文:https://www.cnblogs.com/bitpeng/p/4748872.html;
https://blog.csdn.net/feixuedongji/article/details/82984583
python --- request返回值乱码问题的更多相关文章
- shell如何向python传递参数,shell如何接受python的返回值
1.shell如何向python传递参数 shell脚本 python $sendmailCommandPath $optDate python脚本 lastDateFormat = sys.argv ...
- python函数返回值
2016-08-09 15:01:38 python函数返回值使用return语句,可以返回任意类型的数.如果return语句执行,它之后的所有语句都不再执行. def func(x,y): pri ...
- Python 函数返回值
本章详细介绍 返回值: 0x 00 返回值简介 0x 01 指定返回值与隐含返回值 0x 02 return 语句位置与多条 return 语句 0x 03 返回值类型 0x 04 函数嵌套 0x 0 ...
- Python丢弃返回值
函数多个返回值 python的函数支持返回多个值.返回多个值时,默认以tuple的方式返回. 例如,下面两个函数的定义是完全等价的. def f(): return 1,2 def f(): retu ...
- python 函数返回值笔记
今天学习python时候学习到闭包和柯里化 感觉看概念时候不好理解,自己写下大概就明白点了 柯里化如下 定义一个加法函数 def add(x, y): return x + y 这是没有柯里化之前的函 ...
- python的返回值
1.返回值的作用 函数并非总是直接显示输出,相反,它可以处理一些数据,并返回一个或一组值.函数返回的值被称为返回值.在函数中,可使用return语句将值返回到调用函数的代码行.返回值让你能够将程序的大 ...
- Python 函数返回值、作用域
函数返回值 多条return语句: def guess(x): if x > 3: return "> 3" else: return "<= 3&qu ...
- day09 python函数 返回值 参数
day09 python 一.函数 1.函数 函数是对功能的封装 语法: 定义函数: def 函数名(形参): ...
- python多线程返回值的实现与处理
题目: # 编写一个python程序,创建两个子线程,分别到下面的网址获取文本内容# http://mirrors.163.com/centos/6/isos/x86_64/README.txt# h ...
随机推荐
- json序列化(重要)
(1)同(2)public JsonResult JsonUserGet() { DataSet ds = Web_User.P_LG_User_Get(nUserId); return Json(J ...
- Gridview的stretchMode详解附自动宽度
<GridView android:id="@+id/grid" android:layout_width="fill_parent" android:l ...
- SIAMATIC S7-1200 中通过 Modbus RTU 如何读取地址范围 9999 到 65535 的输入字
原文地址 说明 除了需要 STEP 7 >= V13 SP1 (TIA Portal) 的软件,还需要 S7-1200 CPU 固件版本 >= V4 (文章编号: 6ES721x-1xx4 ...
- mysql 三表索引优化
建表语句 CREATE TABLE IF NOT EXISTS `phone`( `phoneid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `card` ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 图片:缩略图功能
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:"text-primary" 类的文本样式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- git 的一些基本命令
基本命令 1.返回上一级目录:cd ../ 2.进入某一目录:cd git (进入 git 目录) 3. 显示当前路径:pwd 4.显示当前文件目录的文件 : dir 5.新建文件夹:mkdir +文 ...
- 2-10 就业课(2.0)-oozie:4、通过oozie执行shell脚本
oozie的配置文件job.properties:里面主要定义的是一些key,value对,定义了一些变量,这些变量往workflow.xml里面传递workflow.xml :workflow的配置 ...
- exchange 强制更新全球通讯簿
————-客户端强制更新方式————– Outlook通讯录默认情况需要2-3天同步更新通讯录,可以使用下列方式立即更新通讯录 1. 关闭outlook ,打开下列文件夹 %userprofile%\ ...
- other#docker
阿里云docker镜像加速地址:https://cr.console.aliyun.com/#/accelerator docker 安装: yum install -y yum-utils devi ...