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

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

问题描述

尝试在Python中以请求获取HTTP响应内容的原始数据。我有兴趣通过另一个频道转发回复,这意味着理想情况下内容应尽可能保持原始状态。

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?

非常感谢!

推荐答案

如果您使用 requests.get 调用获取HTTP响应,您可以使用响应的 raw 属性。以下是 requests docs

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)
'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'

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

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