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

强颜欢笑 提交于 2021-02-15 10:41:19

问题


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?


回答1:


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!




回答2:



import requests

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

site_response = str(site_request.content)

print(site_response)

You can do it either way.



来源:https://stackoverflow.com/questions/9029287/how-to-extract-http-response-body-from-a-python-requests-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!