Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 935
Stephen Gross Avatar asked Jan 27 '12 05:01

Stephen Gross


People also ask

How do you check body responses in Python?

Simple use requests. get() method to get all body content and use response. json() to get JSON data.

How do I print a response body?

Method Summary Peeks into the JSON that JsonPath will parse by printing it to the console. Peeks into the response body by printing it to the console in a prettified manner. Pretty-print the response body if possible and return it as string. Print the response body and return it as string.


2 Answers

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

like image 79
Robert Peters Avatar answered Sep 20 '22 05:09

Robert Peters


 import requests  site_request = requests.get("https://abhiunix.in")  site_response = str(site_request.content)  print(site_response)  

You can do it either way.

like image 23
Abhijeet Singh Avatar answered Sep 18 '22 05:09

Abhijeet Singh