Requests中文乱码解决方案
分析:
r = requests.get(“http://www.baidu.com“)
**r.text返回的是Unicode型的数据。
使用r.content返回的是bytes型的数据。
也就是说,如果你想取文本,可以通过r.text。
如果想取图片,文件,则可以通过r.content。**
获取一个网页的内容
方法1:使用r.content,得到的是bytes型,再转为str
url='http://music.baidu.com'
r = requests.get(url)
html=r.content
html_doc=str(html,'utf-8') #html_doc=html.decode("utf-8","ignore")
print(html_doc)
方法2:使用r.text
Requests 会自动解码来自服务器的内容。大多数 unicode 字符集都能被无缝地解码。请求发出后,Requests 会基于 HTTP 头部对响应的编码作出有根据的推测。当你访问 r.text 之时,Requests 会使用其推测的文本编码。你可以找出 Requests 使用了什么编码,并且能够使用 r.encoding 属性来改变它.
但是Requests库的自身编码为: r.encoding = ‘ISO-8859-1’
可以 r.encoding 修改编码
url='http://music.baidu.com'
r=requests.get(url)
r.encoding='utf-8'
print(r.text)
获取一个网页的内容后存储到本地
方法1:r.content为bytes型,则open时需要open(filename,”wb”)
r=requests.get("music.baidu.com")
html=r.content
with open('test5.html','wb') as f:
f.write(html)
方法2:r.content为bytes型,转为str后存储
r = requests.get("http://www.baidu.com")
html=r.content
html_doc=str(html,'utf-8') #html_doc=html.decode("utf-8","ignore")
# print(html_doc)
with open('test5.html','w',encoding="utf-8") as f:
f.write(html_doc)
方法3:r.text为str,可以直接存储
r=requests.get("http://www.baidu.com")
r.encoding='utf-8'
html=r.text
with open('test6.html','w',encoding="utf-8") as f:
f.write(html)
Requests+lxml
# -*-coding:utf8-*-
import requests
from lxml import etree url="http://music.baidu.com"
r=requests.get(url)
r.encoding="utf-8"
html=r.text
# print(html)
selector = etree.HTML(html)
title=selector.xpath('//title/text()')
print (title[0])
结果为:百度音乐-听到极致
终极解决方法
以上的方法虽然不会出现乱码,但是保存下来的网页,图片不显示,只显示文本。而且打开速度慢,找到了一篇博客,提出了一个终极方法,非常棒。
来自博客 http://blog.chinaunix.net/uid-13869856-id-5747417.html 的解决方案:
# -*-coding:utf8-*- import requests req = requests.get("http://news.sina.com.cn/") if req.encoding == 'ISO-8859-1':
encodings = requests.utils.get_encodings_from_content(req.text)
if encodings:
encoding = encodings[0]
else:
encoding = req.apparent_encoding # encode_content = req.content.decode(encoding, 'replace').encode('utf-8', 'replace')
global encode_content
encode_content = req.content.decode(encoding, 'replace') #如果设置为replace,则会用?取代非法字符; print(encode_content) with open('test.html','w',encoding='utf-8') as f:
f.write(encode_content)
以上文章转载于https://blog.csdn.net/chaowanghn/article/details/54889835
Requests中文乱码解决方案的更多相关文章
- aspx页面,中文乱码解决方案
由于文件编码方式编码方式不统一出现样式中文乱码解决方案: 今天碰到的问题:页面字体样式设置的'微软雅黑',可页面没引用.我调试看到样式出现中文乱码了 这种问题,就需要转换文件的编码方式,如下两步即可解 ...
- JSP中pageEncoding和charset区别,中文乱码解决方案(转载)
转载自:JSP中pageEncoding和charset区别,中文乱码解决方案 JSP指令标签中<%@ page contentType="text/html;charset=GB23 ...
- boost::xml——基本操作以及中文乱码解决方案 (续)
本博文主要想说明以下两点: 1.对于上一篇的<boost::xml——基本操作以及中文乱码解决方案>解释,这篇博文基本解决了正确输入输出中英文问题,但是好像还没有解决修改中文出现乱码的问题 ...
- 基于Windows环境下cmd/编译器无法输入中文,显示中文乱码解决方案
基于Windows环境下cmd/编译器无法输入中文,显示中文乱码解决方案 两个月前做C++课设的时候,电脑编译器编译结果出现了中文乱码,寻求了百度和大神们,都没有解决这个问题,百度上一堆解释是对编译器 ...
- JS传值中文乱码解决方案
JS传值中文乱码解决方案 一.相关知识 1,Java相关类: (1)java.net.URLDecoder类 HTML格式解码的实用工具类,有一个静态方法:public static String ...
- Java中文乱码解决方案
Java中文乱码解决方案 1.中文乱码解决方案,确保每个文件的默认编码是UTF-8 加入 URIEncoding="UTF-8" 代码中的设置 1>在se ...
- Eclipse中文乱码解决方案
Eclipse中文乱码解决方案 1)第一个设置:window>perferences>general>workspace>text file encoding 2)Jsp编码问 ...
- (转)JSP HTML JAVASCRIPT 中文乱码 解决方案 大全
JSP HTML JAVASCRIPT 中文乱码 解决方案 大全 JSP的中文字符一直是各位初学者首先要解决的问题,下面进行了总结,也给出了解决办法.C4.1 HTML中文编码转换 在JSP文件中的静 ...
- AJAX中文乱码解决方案
通过AJAX获取数据中文乱码解决方案: @ResponseBody 作用: 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到 ...
随机推荐
- [转]Repeat Page Header on each Page for reports SSRS
本文转自:https://stackoverflow.com/questions/3475144/i-want-to-repeat-page-header-on-each-page-for-repor ...
- [转]IIS的各种身份验证详细测试
本文转自:http://www.cnblogs.com/chnking/archive/2007/11/20/965553.html#_Toc183326163 一. IIS的身份验证概述 1. ...
- IIS 站点 共享目录
1.先建立站点,再设置文件夹为共享,Everyone 2.Mac电脑 Everyone不能访问,必须建立用户
- SSM实现图片上传管理操作
Spring MVC 实现文件上传 时序图 利用 Spring MVC 实现文件上传功能,离不开对 MultipartResolver 的设置.MultipartResolver 这个类,你可以将其视 ...
- 使用dom4j写xml文件——源码
1 dom4j下载与配置 1.1 dom4j下载 请移步下载链接 1.2 maven依赖 <dependency> <groupId>org.dom4j</groupId ...
- java ThreadLocal(应用场景及使用方式及原理)
尽管ThreadLocal与并发问题相关,可是很多程序猿只将它作为一种用于"方便传參"的工具,胖哥觉得这或许并非ThreadLocal设计的目的,它本身是为线程安全和某些特定场景的 ...
- spss C# 二次开发 学习笔记(二)——Spss以及统计术语解释(IT人眼中的统计术语)
针对客户需求,需要对一些数据做统计分析.统计分析的第一步,即为数据查询,查找出要统计分析的数据. 查询得出的是一个行列表格的结果集,行.列.表格等这些IT的数据库概念和Spss以及统计中的术语是如何对 ...
- Java SE 8 的流库学习笔记
前言:流提供了一种让我们可以在比集合更高的概念级别上指定计算的数据视图.如: //使用foreach迭代 long count = 0; for (String w : words) { if (w. ...
- webpack-bundle.js原理
bundle.js 源码 //a.js import { log } from './b.js' log('hello') //b.js export const log = function (m) ...
- Html5 填表 表单(二) input type 各种输入, 各种用户选择,上传等等泛输入用户交互
<input> 无限制输入 type 限制输入 type = 如下类型 type 后还可以跟一些属性: 如<input type=text max ...