【问题标题】:How do I read a response from Python Requests?如何阅读 Python 请求的响应?
【发布时间】:2013-09-19 14:02:51
【问题描述】:

我有两个 Python 脚本。一种使用Urllib2 library,一种使用Requests library。

我发现 Requests 更容易实现,但我找不到 urlib2 的 read() 函数的等效项。例如:

...
response = url.urlopen(req)
print response.geturl()
print response.getcode()
data = response.read()
print data

一旦我建立了我的帖子 url,data = response.read() 就会给我内容 - 我正在尝试连接到 vcloud director api 实例,并且响应显示我可以访问的端点。但是,如果我按如下方式使用 Requests 库.....

....

def post_call(username, org, password, key, secret):

    endpoint = '<URL ENDPOINT>'
    post_url = endpoint + 'sessions'
    get_url = endpoint + 'org'
    headers = {'Accept':'application/*+xml;version=5.1', \
               'Authorization':'Basic  '+ base64.b64encode(username + "@" + org + ":" + password), \
               'x-id-sec':base64.b64encode(key + ":" + secret)}
    print headers
    post_call = requests.post(post_url, data=None, headers = headers)
    print post_call, "POST call"
    print post_call.text, "TEXT"
    print post_call.content, "CONTENT"
    post_call.status_code, "STATUS CODE"

....

....print post_call.text 和 print post_call.content 不返回任何内容,即使请求后调用中的状态代码等于 200。

为什么我的 Requests 响应没有返回任何文本或内容?

【问题讨论】:

  • 您知道应该从 URL 中获得的响应类型吗? json、xml等?您从 urllib2 得到的响应是什么?
  • POST 请求可能正在返回重定向响应。检查响应头:post_call.headers

标签: python python-requests


【解决方案1】:

Requests 没有对应于 Urlib2 的 read()。

>>> import requests
>>> response = requests.get("http://www.google.com")
>>> print response.content
'<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage"><head>....'
>>> print response.content == response.text
True

您发出的 POST 请求似乎没有返回任何内容。这通常是 POST 请求的情况。也许它设置了一个cookie?状态码是告诉你 POST 毕竟成功了。

为 Python 3 编辑:

Python now handles data types differently。 response.content 返回一个 bytes 序列(表示 ASCII 的整数),而 response.text 是一个 string (字符序列)。

因此,

>>> print response.content == response.text
False

>>> print str(response.content) == response.text
True

【讨论】:

  • 好的 - 谢谢。也许我在某个地方感到困惑。 urllib2 向我展示了内容,因此我需要了解我做错了什么以及两个库之间的不同调用。
  • 在某个端点我想读取请求,但我无法使用 requests.get("url")。此外,它需要不合理的时间来执行。不提供参数也会引发错误,提示需要 1 个参数。
  • 检查响应代码。您可能会收到超时而不是 2XX 响应。这也可以解释为什么需要这么长时间。
  • 完美解决方案。你节省了我的时间。谢谢你
  • 在 Python 3 中,response.content 是 Bytes 实例,response.text 是 str,因此它们将不再直接比较相等(但使用正确编码解码 response.content 应该返回response.text
【解决方案2】:

如果响应是 json 格式,您可以执行类似 (python3) 的操作:

import json
import requests as reqs

# Make the HTTP request.
response = reqs.get('http://demo.ckan.org/api/3/action/group_list')

# Use the json module to load CKAN's response into a dictionary.
response_dict = json.loads(response.text)

for i in response_dict:
    print("key: ", i, "val: ", response_dict[i])

要查看回复中的所有内容,您可以使用.__dict__:

print(response.__dict__)

【讨论】:

    【解决方案3】:

    如果您将图像推送到某些 API 并希望返回结果地址(响应),您可以这样做:

    import requests
    url = 'https://uguu.se/api.php?d=upload-tool'
    data = {"name": filename}
    files = {'file': open(full_file_path, 'rb')}
    response = requests.post(url, data=data, files=files)
    current_url = response.text
    print(response.text)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多