如何使用 Python 在请求中获取响应的原始内容? [英] How to get the raw content of a response in requests with Python?

查看:42
本文介绍了如何使用 Python 在请求中获取响应的原始内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在Python中获取requests中HTTP响应内容的原始数据.我有兴趣通过另一个渠道转发响应,这意味着理想情况下,内容应尽可能原始.

这样做的好方法是什么?

解决方案

如果您使用 requests.get 调用来获取 HTTP 响应,您可以使用 raw> 响应的属性.这是requests文档中的代码.

<预><代码>>>>r = requests.get('https://github.com/timeline.json', stream=True)>>>r.raw<requests.packages.urllib3.response.HTTPResponse 对象在 0x101194810>>>>r.raw.read(10)'x1fx8bx08x00x00x00x00x00x00x03'

Trying to get the raw data of the HTTP response content in requests in Python. I am interested in forwarding the response through another channel, which means that ideally the content should be as pristine as possible.

What would be a good way to do this?

解决方案

If you are using a requests.get call to obtain your HTTP response, you can use the raw attribute of the response. Here is the code from the requests docs.

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

这篇关于如何使用 Python 在请求中获取响应的原始内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆