当使用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

好了,这下就能解决这个问题了。哎,这个小鬼挺能折腾的。

  

最新文章

  1. STM32之独立看门狗与窗口看门狗总结
  2. DeviceFamily XAML Views(一)
  3. 解决用navicate远程连接数据库出现1045 access denied for user 'root'@'localhost' using password yes
  4. 转:this的用法
  5. PKCS #1 RSA Encryption Version 1.5
  6. 1028 - Carl the Ant
  7. ### Caffe
  8. sql server 2008数据复制
  9. poj1543---完美立方(枚举)
  10. python读取word表格内容(1)
  11. dom 规划(html和xml)
  12. rsync+inotify实现数据的实时备份
  13. 记一次服务器Tomcat优化经历
  14. Android轶事之View要去大保健?View大小自己决定?
  15. 类似Jquery ui 标签页(Tabs)
  16. Django web编程1 -- 创建项目和应用
  17. java 导出
  18. springboot缓存注解——@CachePut
  19. Unity Button事件的简洁处理
  20. vue上传图片

热门文章

  1. .NET Mvc中ViewBag、ViewData、TempData如何使用
  2. hibernate主键生成策略
  3. python读取caffemodel文件
  4. 【bzoj1004】 HNOI2008—Cards
  5. Nike Zoom Winflo 2 Kvinder Sko Når jeg set elementet
  6. AJAX应用小案例
  7. tomcat 动态部署
  8. C语言产生标准正态分布或高斯分布随机数
  9. Visual SVN 5.01 Po jie 笔记
  10. MySQL性能优化:索引