您好,欢迎来到要发发知识网。
搜索
您的当前位置:首页python requests响应内容的三种方法

python requests响应内容的三种方法

来源:要发发知识网

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学习指路:

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

Copyright © 2019- net188.cn 版权所有 湘ICP备2022005869号-2

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

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