当使用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 乱码的更多相关文章

  1. 爬虫学习之-requests乱码

    总体功能的一个演示 import requests response = requests.get("https://www.baidu.com") print(type(resp ...

  2. requests乱码问题

    有三种方法解决请求后乱码问题. 一:获取二进制数据,再利用str进行编码转换 url='http://music.baidu.com' r = requests.get(url) html=r.con ...

  3. java web 学习十(HttpServletRequest对象1)

    一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...

  4. python(27)requests 爬取网页乱码,解决方法

    最近遇到爬取网页乱码的情况,找了好久找到了种解决的办法: html = requests.get(url,headers = head) html.apparent_encoding html.enc ...

  5. Python HTTP库requests中文页面乱码解决方案!

    http://www.cnblogs.com/bitpeng/p/4748872.html Python中文乱码,是一个很大的坑,自己不知道在这里遇到多少问题了.还好通过自己不断的总结,现在遇到乱码的 ...

  6. Python3的requests类抓取中文页面出现乱码的解决办法

      这种乱码现象基本上都是编码造成的,我们要转到我们想要的编码,先po一个知识点,嵩天老师在Python网络爬虫与信息提取说到过的:response.encoding是指从HTTP的header中猜测 ...

  7. Requests中文乱码解决方案

    分析: r = requests.get(“http://www.baidu.com“) **r.text返回的是Unicode型的数据. 使用r.content返回的是bytes型的数据. 也就是说 ...

  8. 解决requests获取源代码时中文乱码问题

    用requests获取源代码时,如果是中文网页,就可能会出现乱码,下面我以中关村的网站为例: import requests url = 'http://desk.zol.com.cn/meinv/' ...

  9. python中requests库中文乱码问题

    当使用这个库的时候经常会出现各种乱码的情况. 首先要知道: text返回的是处理过的unicode的数据. content返回的是bytes的原始数据 也就是说r.content比r.text更加节省 ...

随机推荐

  1. SpringMVC的小总结

    ---恢复内容开始--- 前言: springMVC是我接触的第一个框架,当时在学校学习的时候还是各种懂,最简单的springMVC框架的配置还是比较熟,后来工作之后,虽然主要用的确实是springM ...

  2. 一次kibana服务失败的排查过程

    公司在kubernetes集群上稳定运行数月的kibana服务于昨天下午突然无法正常提供服务,访问kibana地址后提示如下信息: 排查过程: 看到提示后,第一反应肯定是检查elasticsearch ...

  3. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  4. ORacle修改表列长度

    alter table 表名 modify column_name varchar2(32) alter table 表名 modify (column_name1 varchar(20) defau ...

  5. oracle存储过程

    1.存储过程定义 储存程序 (Stored Procedure),又可称预储程序或者存储过程,是一种在数据库中存储复杂程序,以便外部程序调用的一种数据库对象,它可以视为数据库中的一种函数或子程序.-- ...

  6. Web系统大规模并发——电商秒杀与抢购

    电商的秒杀和抢购,对我们来说,都不是一个陌生的东西.然而,从技术的角度来说,这对于Web系统是一个巨大的考验.当一个Web系统,在一秒钟内收到数以万计甚至更多请求时,系统的优化和稳定至关重要.这次我们 ...

  7. [Bootstrap-插件使用]Jcrop+fileinput组合实现头像上传功能

    很久没有更新博客了,再不写点东西都烂了. 这次更新一个小内容,是两个插件的组合使用,实现头像上传功能. 业务需求: 头像上传功能,要对上传的文件进行剪切,且保证头像到服务器时必须是正方形的. 优化&l ...

  8. spring batch资料收集

    spring batch官网 Spring Batch在大型企业中的最佳实践 一篇文章全面解析大数据批处理框架Spring Batch Spring Batch系列总括

  9. pageX、clientX、screenX、offsetX、layerX、x

    参考:http://www.cnblogs.com/xesam/archive/2011/12/08/2280509.html chrome: e.pageX--相对整个页面的坐标e.layerX-- ...

  10. Java 随机抽奖

    package Third; import java.util.Scanner; public class LotteryOdds { public static void main(String[] ...