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更加节省 ...
随机推荐
- HAproxy的安装与配置讲解
1,安装 官网下载 http://www.haproxy.org/#down cd /usr/local/src/ wget http://www.haproxy.org/download/1.4/s ...
- javaweb查看后台session和request所有的值
遍历session @RequestMapping(value ="/test2") public String upload2( HttpSession session) { E ...
- Beta阶段项目终审报告
先上图 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件主要是用来解决玩狼人杀这款桌游时无牌.无法官.游戏流程不熟悉等情况的.我觉得我们对典型 ...
- linux动态代码注入
参考网上文章:http://www.freebuf.com/articles/system/6388.html 按照文章,实现了代码的动态注入,即对一个正在运行的进程,在不重启的情况下执行一段不在原程 ...
- 14 Generator
Generator 就是可以返回多个结果,也是支持 Iterator 接口. function* helloWorldGenerator() { yield 'hello'; yield 'world ...
- ios 快速审核
https://developer.apple.com/contact/app-store/?topic=expedite
- Linux下Steam中支持中文的办法
搜索过好几个解决方案,诸如添加skin等等,在我的ARCH机器上似乎都不行然后在搜索linux steam cjk时, 发现一个链接中有解决DOTA2显示中文不正确的问题,感觉可能有用,就参考着搞定了 ...
- python之登录小程序
# 登录操作 PassWord_list = [] Reset_pw = '*#*#' def account_login(): if PassWord_list == []: PassWord = ...
- Windows10配置NFS服务端和客户端
环境:Windows10企业版x64 安装服务端 安装hane win nfs server,版本1169(官方最新版1223经试验不成功). 设置如下: 以管理员身份重启服务(注:软件界面上&quo ...
- Python之路【第二十三篇】爬虫
difference between urllib and urllib2 自己翻译的装逼必备 What is the difference between urllib and urllib2 mo ...