【问题标题】:How to extract HTTP response body from a Python requests call?如何从 Python 请求调用中提取 HTTP 响应正文?
【发布时间】:2012-02-20 04:56:12
【问题描述】:

我正在使用 Python 请求库。我试图弄清楚如何从响应中提取实际的 HTML 正文。代码看起来有点像这样:

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

这确实应该打印很多内容,但什么也不打印。

有什么建议吗?也许我误解了 requests.get() 的工作原理?

【问题讨论】:

    标签: http-request python-requests


    【解决方案1】:

    你可以试试这个方法:

    import requests
    
    response = requests.get("http://www.google.com")
    response.raise_for_status()
    
    data = response.json()
    print(data)
    

    【讨论】:

      【解决方案2】:
      
      import requests
      
      site_request = requests.get("https://abhiunix.in")
      
      site_response = str(site_request.content)
      
      print(site_response)
      
      

      无论哪种方式都可以。

      【讨论】:

        【解决方案3】:

        您的代码是正确的。我测试过:

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

        它返回了大量内容。 检查网址,尝试“http://www.google.com”。干杯!

        【讨论】:

        • 是的,没错。我一定误解了我对我正在使用的特定页面的期望。不过,谢谢。
        猜你喜欢
        • 2019-02-09
        • 1970-01-01
        • 2016-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-04
        • 2016-03-12
        • 1970-01-01
        相关资源
        最近更新 更多