157

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?

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
Stephen Gross
  • 5,274
  • 12
  • 41
  • 59

3 Answers3

245

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

starball
  • 20,030
  • 7
  • 43
  • 238
Robert Peters
  • 3,814
  • 1
  • 17
  • 9
  • 1
    Yeah, that's true. I must be misunderstanding what I should expect from the particular page I'm working with. Thanks, though. – Stephen Gross Jan 27 '12 at 05:40
18

You can try this method:

import requests

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

data = response.json()
print(data)
Olerato
  • 196
  • 1
  • 4
14

import requests

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

site_response = str(site_request.content)

print(site_response)

You can do it either way.

Community
  • 1
  • 1
Abhijeet Singh
  • 171
  • 1
  • 6