How to extract HTTP response body from a Python requests call?
Asked Answered
V

3

168

I'm using the Python requests library. I'm trying to figure out how to extract the actual HTML body from a response. The code looks a bit like this:

r = requests.get(...)
print r.content

This should indeed print lots of content, but instead prints nothing.

Any suggestions? Maybe I've misunderstood how requests.get() works?

Villiform answered 27/1, 2012 at 5:19 Comment(0)
H
260

Your code is correct. I tested:

r = requests.get("http://www.google.com")
print(r.content)

And it returned plenty of content. Check the url, try "http://www.google.com".

Hesperides answered 27/1, 2012 at 5:33 Comment(1)
Yeah, that's true. I must be misunderstanding what I should expect from the particular page I'm working with. Thanks, though.Villiform
S
19

You can try this method:

import requests

response = requests.get("http://www.google.com")
response.raise_for_status()

data = response.json()
print(data)
Sagamore answered 21/8, 2021 at 20:31 Comment(1)
The json() method only works if the response body is in JSON formatGumm
S
17

import requests

site_request = requests.get("https://abhiunix.in")

site_response = str(site_request.content)

print(site_response)

You can do it either way.

Seif answered 1/5, 2020 at 22:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.