python requests的content和text方法的区别

来源:互联网 发布:linux groupadd 编辑:程序博客网 时间:2024/05/29 15:19

一直在想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.        """    #content的完整代码就不贴了。    @property    def content(self):        """Content of the response, in bytes."""
结论是:text 返回的是unicode 型的数据,一般是在网页的header中定义的编码形式。

content返回的是bytes,二级制型的数据。

也就是说你如果想要提取文本就用text

但是如果你想要提取图片、文件,就要用到content

举例说明:

# 例如下载并保存一张图片import requestsjpg_url = 'http://img2.niutuku.com/1312/0804/0804-niutuku.com-27840.jpg'content = requests.get(jpg_url).contentwith open('demo.jpg', 'wb') as fp:    fp.write(content)