您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页Requests库里的text和content属性的区别

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

来源:二三四教育网

官方的文档说明:

>>>>>>>>>>>>>>>>>>>>>>>>>>>  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 = 
r = requests.get(image_url)
content = r.content
with open('image.jpg', 'wb') as f:
    f.write(content)

Copyright © 2019- how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务