Python的requestcontent和text的区别
2016-10-14 15:50
357 查看
requests的content和text的区别
一般情况下text提取出来的可能乱码,content提取出来的一般不会乱码。
一直在想requests的content和text的区别,从print 结果来看是没有任何区别的
那两者之间有什么不同,看下源码:
[/code]
resp.text返回的是Unicode型的数据。
使用resp.content返回的是bytes型的数据。
也就是说,如果你想取文本,可以通过r.text。
如果想取图片,文件,则可以通过r.content。
[/code]
不过下面的人也给出了解决办法:
首先将源代码转化成比特数组,然后再将比特数组转化成一个比特对象。这样就可以绕过这个bug。
然而,又有人认为这不是一个bug, 所以一直没有被修复。这是由于,我获取源代码是使用r.text
而如果使用r.content:
就不会报错。
那r.text与r.content有什么区别呢?分析requests的源代码发现,r.text返回的是Unicode型的数据,而使用r.content返回的是bytes型的数据。也就是说,在使用r.content的时候,他已经只带了
这样一个转化了。
最近CentOS都声明放弃Python2了,编码问题确实浪费了很多时间,等空下来转Python3吧~
一般情况下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”>
[/code]
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))
[/code]
不过下面的人也给出了解决办法:
html = bytes(bytearray(html, encoding='utf-8')) selector = etree.HTML(html)
首先将源代码转化成比特数组,然后再将比特数组转化成一个比特对象。这样就可以绕过这个bug。
然而,又有人认为这不是一个bug, 所以一直没有被修复。这是由于,我获取源代码是使用r.text
html = requests.get('xxxxxx',cookies=cookies).text
而如果使用r.content:
html = requests.get('xxxxxx',cookies=cookies).content
就不会报错。
那r.text与r.content有什么区别呢?分析requests的源代码发现,r.text返回的是Unicode型的数据,而使用r.content返回的是bytes型的数据。也就是说,在使用r.content的时候,他已经只带了
html = bytes(bytearray(html, encoding='utf-8'))
这样一个转化了。
最近CentOS都声明放弃Python2了,编码问题确实浪费了很多时间,等空下来转Python3吧~
相关文章推荐
- python_requests的content和text方法的区别
- python中response.text与response.content的区别
- python requests的content和text方法的区别
- python response.text和response.content的区别
- 对python requests的content和text方法的区别详解
- python requests的content和text方法的区别(转)
- python中requests库content和text的区别
- python requests的content和text方法的区别
- python requests 详细学习content 和text 的区别
- response.setContentType与 request.setCharacterEncoding 区别
- response.setContentType与 request.setCharacterEncoding 区别
- AFNetworking request failed unacceptable content type text/html
- Python中的plisttext和HTTP的Content-Type
- getRequestURI,getRequestURL的区别 http://www.360doc.com/content/13/0416/17/1030755_278676005.shtml
- Python 中requests 模块text 和content 差别
- AFNetWorking 错误Request failed: unacceptable content-type: text/html"
- NSLocalizedDescription=Request failed: unacceptable content-type: text/html 解决方法
- response.setContentType与 request.setCharacterEncoding 区别
- <%@page contentType="text/html;charset=gbk"%>与<meta http-equiv="Content-Type" content="text/html; charset=GBK">区别
- AFNetworking2.0请求数据时出现错误Requestfailed:unacceptablecontent-type:text/html解决方法