Requests 乱码
当使用Requests请求网页时,出现下面图片中的一些乱码,我就一脸蒙逼。
程序是这样的。
def getLinks(articleUrl):
headers = {
"Uset-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.1.0.14126"
}
wb_data = requests.get(articleUrl,headers=headers)
bsObj = BeautifulSoup(wb_data.text,"lxml")
return bsObj
程序的中出现的乱码图片是这样的。

怎么解决呢?好在有google大神,让我找到了一些前辈写的博客,拿去看吧,^_^。
http://blog.chinaunix.net/uid-13869856-id-5747417.html
http://blog.csdn.net/a491057947/article/details/47292923#t1
还有官网链接。两个地方都有讲到。(偷偷告诉你有chinese版本的,自己去找吧)
http://docs.python-requests.org/en/latest/user/quickstart/#response-content
http://docs.python-requests.org/en/master/user/advanced/#compliance
英文不好,我们来看看中文版的说的是什么,见下图。


好了,资料看完了,总结一下吧。
解决思路:
1.见到有乱码,不用怕,首先我们来看看编码方式是什么?怎么看?把编码方式打印出来看看。
def getLinks(articleUrl):
headers = {
"Uset-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.1.0.14126"
}
wb_data = requests.get(articleUrl,headers=headers)
bsObj = BeautifulSoup(wb_data.text,"lxml")
hrefs = bsObj.find("div",{"class":"booklist clearfix"})
print(wb_data.headers['content-type'])
print(wb_data.encoding) # response的内容编码
print(wb_data.apparent_encoding) #response headers 里设置的编码
print(requests.utils.get_encodings_from_content(wb_data.text)) #response返回的html header标签里设置的编码
return bsObj
返回的是这些个鬼东西。
text/html
ISO-8859-1 # response的内容编码
UTF-8-SIG #response headers 里设置的编码
['utf-8'] #response返回的html header标签里设置的编码
这下知道为啥乱码了,原来是response的内容编码和response headers 里设置的编码不一样啊。
2.怎么办呢?不一样,那我们就改成一样的。改变response的内容编码格式。
有两种方法:
(1)使用.encoding属性改变response的内容编码,在代码里加上下面一行代码。
wb_data.encoding = 'utf-8' #手动指定编码方式
def getLinks(articleUrl):
headers = {
"Uset-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.1.0.14126"
}
wb_data = requests.get(articleUrl,headers=headers)
wb_data.encoding = 'utf-8' #手动指定编码方式
bsObj = BeautifulSoup(wb_data.text,"lxml")
return bsObj
(2)使用原始的Response.content
bsObj = BeautifulSoup(wb_data.text,"lxml")
#将wb_data.text改为wb_data.content
bsObj = BeautifulSoup(wb_data.content,"lxml")
3.从前面链接里就可以看到,一位前辈写出了下面代码。解决这类问题,一劳永逸的方法。
我给应用到我的代码里,看看可行不?^_^。
原理是这样的,当response内容的编码是'ISO-8859-1',首先查找返回的Html的header标签里设置的编码;如果此编码不存在,查看response header设置的编码
def getLinks(articleUrl):
headers = {
"Uset-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.1.0.14126"
}
wb_data = requests.get(articleUrl,headers=headers) if wb_data.encoding == 'ISO-8859-1':
encodings = requests.utils.get_encodings_from_content(wb_data.text)
if encodings:
encoding = encodings[0]
else:
encoding = wb_data.apparent_encoding
encode_content = wb_data.content.decode(encoding,'replace').encode('utf-8','replace') bsObj = BeautifulSoup(encode_content,"lxml")
return bsObj
好了,这下就能解决这个问题了。哎,这个小鬼挺能折腾的。
Requests 乱码的更多相关文章
- 爬虫学习之-requests乱码
总体功能的一个演示 import requests response = requests.get("https://www.baidu.com") print(type(resp ...
- requests乱码问题
有三种方法解决请求后乱码问题. 一:获取二进制数据,再利用str进行编码转换 url='http://music.baidu.com' r = requests.get(url) html=r.con ...
- java web 学习十(HttpServletRequest对象1)
一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...
- python(27)requests 爬取网页乱码,解决方法
最近遇到爬取网页乱码的情况,找了好久找到了种解决的办法: html = requests.get(url,headers = head) html.apparent_encoding html.enc ...
- Python HTTP库requests中文页面乱码解决方案!
http://www.cnblogs.com/bitpeng/p/4748872.html Python中文乱码,是一个很大的坑,自己不知道在这里遇到多少问题了.还好通过自己不断的总结,现在遇到乱码的 ...
- Python3的requests类抓取中文页面出现乱码的解决办法
这种乱码现象基本上都是编码造成的,我们要转到我们想要的编码,先po一个知识点,嵩天老师在Python网络爬虫与信息提取说到过的:response.encoding是指从HTTP的header中猜测 ...
- Requests中文乱码解决方案
分析: r = requests.get(“http://www.baidu.com“) **r.text返回的是Unicode型的数据. 使用r.content返回的是bytes型的数据. 也就是说 ...
- 解决requests获取源代码时中文乱码问题
用requests获取源代码时,如果是中文网页,就可能会出现乱码,下面我以中关村的网站为例: import requests url = 'http://desk.zol.com.cn/meinv/' ...
- python中requests库中文乱码问题
当使用这个库的时候经常会出现各种乱码的情况. 首先要知道: text返回的是处理过的unicode的数据. content返回的是bytes的原始数据 也就是说r.content比r.text更加节省 ...
随机推荐
- Android App的设计架构:MVC,MVP,MVVM与架构经验谈
相关:http://www.cnblogs.com/wytiger/p/5996876.html 和MVC框架模式一样,Model模型处理数据代码不变在Android的App开发中,很多人经常会头疼于 ...
- 仿QQ大战—界面篇
之前在<仿QQ大战-服务器的搭建(ServerSocket)>中实现了服务器的搭建,以及一个简单地传递数据的实现,现在就是来实现类似与QQ聊天通信的功能.首先是界面的实现: 首先:服务器和 ...
- 一篇对iOS音频比较完善的文章
转自:http://www.cnblogs.com/iOS-mt/p/4268532.html 感谢作者:梦想通 前言 从事音乐相关的app开发也已经有一段时日了,在这过程中app的播放器几经修改我也 ...
- 个人CKeditor的config.js配置
/** * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. * For lic ...
- 常用的Jquery插件
0.模块化前端框架(http://www.layui.com) 1.拖拽滑动验证码(http://www.geetest.com/,https://github.com/dyh1995/jquery. ...
- WinForm------DateEdit属性设置
1.只能选择年份属性设置
- js正则表达式的一些研究,截取两个字符串中间的字符串
一个最常用的场景 截取两个字符串中间的字符串 var str = "iid0000ffr"; var substr = str.match(/id(\S*)ff/); ...
- 【IOS】模仿"抽屉新热榜"动态启动页YFSplashScreen
IOS最好要设置系统默认启动页面,不然进入应用就会突然闪现黑色画面 下图是我们要实现的效果: 总体思路:设置一个系统默认启动页面,在进入didFinishLaunchingWithOptions时, ...
- 如何发布带静态资源的库——android 篇
1.首先要使用 android sdk 提供的命令行工具处理已有的项目: cd YourProjectDir android update project -p ./ 2.上一步生成的 build.x ...
- Java Web ——http协议响应报文
HTTP 响应报文 HTTP 响应报文由状态行.响应头部.空行 和 响应包体 4 个部分组成,如下图所示: 下面对响应报文格式进行简单的分析: 状态行:状态行由 HTTP 协议版本字段.状态码和状态码 ...