导语:
本文主要介绍了关于python requests响应内容的三种方法的相关知识,包括python 类型,以及python bs4这些编程知识,希望对大家有参考作用。
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'
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ python如何创建UDP服务器和客户端?10/28
- ♥ python写入文件时如何覆盖原始写入10/02
- ♥ python中argparse库是什么09/29
- ♥ python2.7不等号是什么01/08
- ♥ python list有什么特点09/23
- ♥ 为什么要用python301/11
内容反馈