美文网首页生活不易 我用python
Requests库里的text和content属性的区别

Requests库里的text和content属性的区别

作者: 東飛 | 来源:发表于2017-04-15 12:13 被阅读128次

官方的文档说明:

>>>>>>>>>>>>>>>>>>>>>>>>>>>  text   <<<<<<<<<<<<<<<<<<<<<<<<<<<
 @property
    def text(self):
        """Content of the response, in unicode."""
                ·········


>>>>>>>>>>>>>>>>>>>>>>>>>>>  content  <<<<<<<<<<<<<<<<<<<<<<<<<<<
@property
    def content(self):
        """Content of the response, in bytes."""
                ·········

  • response.text返回的是Unicode型的数据。
  • response.content返回的是bytes型,也就是二进制的数据。
  • 如果取文本,可以通过r.text。
  • 如果取图片和文件,则可以通过r.content。

例如:

# 下载保存一张图片

# -*- coding:utf-8 -*-
import requests

image_url = 'http://img.infinitynewtab.com/wallpaper/881.jpg'
r = requests.get(image_url)
content = r.content
with open('image.jpg', 'wb') as f:
    f.write(content)

相关文章

网友评论

    本文标题:Requests库里的text和content属性的区别

    本文链接:https://www.haomeiwen.com/subject/prynattx.html