requests的content和text的区别
2018-01-25 09:46
295 查看
转自:http://blog.csdn.net/geekleee/article/details/52816268
requests的content和text的区别
一般情况下text提取出来的可能乱码,content提取出来的一般不会乱码。
一直在想requests的content和text的区别,从print 结果来看是没有任何区别的
那两者之间有什么不同,看下源码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
resp.text返回的是Unicode型的数据。
使用resp.content返回的是bytes型的数据。
也就是说,如果你想取文本,可以通过r.text。
如果想取图片,文件,则可以通过r.content。
2
3
4
5
6
7
不过下面的人也给出了解决办法:
2
首先将源代码转化成比特数组,然后再将比特数组转化成一个比特对象。这样就可以绕过这个bug。
然而,又有人认为这不是一个bug, 所以一直没有被修复。这是由于,我获取源代码是使用r.text
而如果使用r.content:
就不会报错。
那r.text与r.content有什么区别呢?分析requests的源代码发现,r.text返回的是Unicode型的数据,而使用r.content返回的是bytes型的数据。也就是说,在使用r.content的时候,他已经只带了
这样一个转化了。
最近CentOS都声明放弃Python2了,编码问题确实浪费了很多时间,等空下来转Python3吧~
requests的content和text的区别
一般情况下text提取出来的可能乱码,content提取出来的一般不会乱码。
一直在想requests的content和text的区别,从print 结果来看是没有任何区别的
那两者之间有什么不同,看下源码:
@property def text(self): """Content of the response, in unicode. If Response.encoding is None, encoding will be guessed using ``chardet``. The encoding of the response content is determined based solely on HTTP headers, following RFC 2616 to the letter. If you can take advantage of non-HTTP knowledge to make a better guess at the encoding, you should set ``r.encoding`` appropriately before accessing this property. """ #完整代码就不贴了。 @property def content(self): """Content of the response, in bytes."""style=”display: none;” class=”save_code tracking-ad” data-mod=”popu_249”>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
resp.text返回的是Unicode型的数据。
使用resp.content返回的是bytes型的数据。
也就是说,如果你想取文本,可以通过r.text。
如果想取图片,文件,则可以通过r.content。
# (例如我们请求一个图片地址并且打开图片的话,就可以使用resp.content:) >>> from PIL import Image >>> from io import StringIO >>> i = Image.open(StringIO(resp.content))1
2
3
4
5
6
7
不过下面的人也给出了解决办法:
html = bytes(bytearray(html, encoding='utf-8')) selector = etree.HTML(html)1
2
首先将源代码转化成比特数组,然后再将比特数组转化成一个比特对象。这样就可以绕过这个bug。
然而,又有人认为这不是一个bug, 所以一直没有被修复。这是由于,我获取源代码是使用r.text
html = requests.get('xxxxxx',cookies=cookies).text1
而如果使用r.content:
html = requests.get('xxxxxx',cookies=cookies).content1
就不会报错。
那r.text与r.content有什么区别呢?分析requests的源代码发现,r.text返回的是Unicode型的数据,而使用r.content返回的是bytes型的数据。也就是说,在使用r.content的时候,他已经只带了
html = bytes(bytearray(html, encoding='utf-8'))1
这样一个转化了。
最近CentOS都声明放弃Python2了,编码问题确实浪费了很多时间,等空下来转Python3吧~
相关文章推荐
- python requests 详细学习content 和text 的区别
- requests入门 response的常用方法 response.text 和response.content的区别
- python_requests的content和text方法的区别
- python requests的content和text方法的区别
- python requests的content和text方法的区别(转)
- python requests的content和text方法的区别
- 对python requests的content和text方法的区别详解
- nodeType、innerHTML、innerText、textContent之间的区别与联系
- xml Node getNodeValue 和 getTextContent 区别
- innerText和textContent区别及浏览器兼容
- <%@page contentType="text/html;charset=GBK"%>与<meta content="text/html; charset=GBK">区别
- textContent、innerHTML、innerText、outerText、outerHTML、nodeValue使用场景和区别
- requests的content与text导致lxml的解析问题
- requests的content与text导致lxml的解析问题
- requests的content与text导致lxml的解析问题
- textContent和innerText属性的区别
- requests的content与text导致lxml的解析问题
- text和content方法的区别
- python中requests库content和text的区别
- python response.text和response.content的区别