• 技术文章 >Python技术 >Python基础教程

    python requests响应内容的三种方法

    小妮浅浅小妮浅浅2021-10-20 09:42:25原创5683

    1、二进制响应内容,以字节的方式访问请求响应体。

    对于非文本请求:

    >>> r.content
    b'[{"repository":{"open_issues":0,"url":"https://github.com/...

    2、JSON响应内容,Requests有内置的JSON解码器,处理JSON数据。

    >>> import requests
    >>> r = requests.get('https://github.com/timeline.json')
    >>> r.json()
    [{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...

    3、原始响应内容,可以访问r.raw。

    如果你确实想这么干,那请你确保在初始请求中设置了stream=True。具体的你可以这么做:

    >>> r = requests.get('https://github.com/timeline.json', stream=True)
    >>> r.raw
    <requests.packages.urllib3.response.HTTPResponse object at 0x101194810>
    >>> r.raw.read(10)
    '\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'

    以上就是python requests响应内容的三种方法,希望对大家有所帮助。更多Python学习指路:python基础教程

    本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

    专题推荐:python requests
    品易云
    上一篇:python requests读取服务器响应 下一篇:python requests发送不同类型的数据

    相关文章推荐

    • python中分支管理策略的实现• python公有成员和私有成员的介绍• python调用父类的三种方法• python中MRO原则的使用• python命令行模式的使用流程• python算术运算符的扩展功能• python有哪些数组叠加函数• python数组分割的函数• python中INF值的介绍• Python requests如何发送请求• python requests读取服务器响应

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网