如何从 Python 请求中读取响应?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18810777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:55:50  来源:igfitidea点击:

How do I read a response from Python Requests?

pythonpython-requests

提问by Oli

I have two Python scripts. One uses the Urllib2 libraryand one uses the Requests library.

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

I have found Requests easier to implement, but I can't find an equivalent for urlib2's read()function. For example:

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

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

Once I have built up my post url, data = response.read()gives me the content - I am trying to connect to a vcloud director api instance and the response shows the endpoints that I have access to. However if I use the Requests library as follows.....

一旦我建立了我的帖子 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"

....

....the print post_call.textand print post_call.contentreturns nothing, even though the status code equals 200 in the requests post call.

....theprint post_call.textprint post_call.content没有返回任何内容,即使状态代码在调用后的请求中等于 200。

Why isn't my response from Requests returning any text or content?

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

采纳答案by aychedee

Requests doesn't have an equivalent to Urlib2's read().

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

It looks like the POST request you are making is returning no content. Which is often the case with a POST request. Perhaps it set a cookie? The status code is telling you that the POST succeeded after all.

看起来您发出的 POST 请求没有返回任何内容。POST 请求通常就是这种情况。也许它设置了一个cookie?状态代码告诉您 POST 最终成功了。

回答by Jortega

If the response is in json you could do something like (python3):

如果响应在 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])


To see everything in the response you can use .__dict__:

要查看响应中的所有内容,您可以使用.__dict__

print(response.__dict__)