Stephen Gross
Stephen Gross

Reputation: 5724

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

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?

Upvotes: 183

Views: 422814

Answers (3)

Olerato
Olerato

Reputation: 326

You can try this method:

import requests

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

try:
    data = response.json()
except requests.JSONDecodeError:
    data = None
print(data)

Note that calling response.json() can raise a JSONDecodeError, which it is best practice to be prepared to catch

Upvotes: 21

Robert Peters
Robert Peters

Reputation: 4104

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".

Upvotes: 273

Abhijeet Singh
Abhijeet Singh

Reputation: 311


import requests

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

site_response = str(site_request.content)

print(site_response)

You can do it either way.

Upvotes: 17

Related Questions