How to get the raw content of a response in reques

2019-01-18 22:54发布

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?

Thanks very much!

2条回答
聊天终结者
2楼-- · 2019-01-18 23:02

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'
查看更多
ら.Afraid
3楼-- · 2019-01-18 23:21

After requests.get(), you can use r.content to extract the raw Byte-type content.

r = requests.get('https://yourweb.com', stream=True)
r.content
查看更多
登录 后发表回答