当使用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. 递推 hdu 1330

    http://www.cnblogs.com/rainydays/archive/2013/01/16/2862235.html 看样例的答案 #include<stdio.h> #inc ...

  2. hibernate入门案例

    最近准备学ssh,今天学了一下hibernate,用的是hibernate4,现在已经出5了:配置文件很容易写错,写配置文件的时候尽量复制. 需要的jar包如下:(jar包我是直接放在项目工程里面了, ...

  3. spring mvc重定向方法

    一.不带参数,直接重定向到另一个地址: 返回String直接跳转,如: @RequestMapping(value = "/filehandle") public String u ...

  4. php 使用函数中遇到的坑之----list

    1. list 把数组中的值赋给一些变量 <?php $info = array('coffee', 'brown', 'caffeine'); // 列出所有变量 list($drink, $ ...

  5. TThread.CreateAnonymousThread() 匿名线程对象的应用

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  6. 创建Mat对象的几种方法

    1.Mat的构造函数 Mat M(行数,列数,数据类型,通道数) eg:M(2,2, CV_8UC3, Scalar(0,0,255)). 2.利用Mat的Create()函数.Mat M; M.cr ...

  7. PHP图片加文字水印和图片水印方法(鉴于李老师博客因没加水印被盗,特搜集的办法。希望能有用!)

    $dst_path = 'dst.jpg'; //创建图片的实例 $dst = imagecreatefromstring(file_get_contents($dst_path)); //打上文字 ...

  8. [Android]学习笔记之布局

    5大布局,其中前3个是常用的,第四个绝对布局已经提示deprecated ![](http://images2015.cnblogs.com/blog/194303/201611/194303-201 ...

  9. 用JMeter测试monggodb的请求

    JMeter测试MongoDB性能有两种方式,一种是利用JMeter直接测试MongoDB[即通过MongoDB协议测试],另一种是写Java代码方式测试MongoDB[即通过java请求测试] 注: ...

  10. php、前端开发(网站建设)环境搭建

    php集成开发环境wampserver,是一款免费开源的软件,下载地址http://www.wampserver.com,由于是国外的网站,打开速度慢,根据自己的电脑选择32位/64位的系统下载.