为什么我会收到错误 AttributeError: 'Response' object has no attribute 'get' in Python2.7?

Dip*_*Das 9 attributeerror python-2.7

我收到错误 AttributeError: 'Response' object has no attribute 'get' 对于我编写的以下代码

def convert_json(self,bucket,userid,imgfilename,field,i):

    bucketName = bucket
    link = "users_"+str(userid)+'/'+imgfilename
    c = S3Connection(self.AWS_ACCESS_KEY_ID,self.AWS_ACCESS_KEY_SECRET)
    p = c.generate_url(expires_in=long(7200),method='GET',bucket=bucketName,key=link,query_auth=True,force_http=False)  
    post_url = "http://someurl"
    wrapper = {"filename":p}
    try:
        response = requests.post(post_url, json=wrapper)
        print response
        if response.status_code == 200:
            text = response.get('description', [])
        else:
            text = []
    except Exception:
        if response.status_code == 200:
            text = response.get('description', [])
        else:
            text = []
    return text
Run Code Online (Sandbox Code Playgroud)

Use*_*716 10

该对象不是字典,因此您不能使用get. 您可能会通过以下任一方式找到所需内容:

  • r.status_code
  • 内容
  • 文本
  • r.json()

引用requests 页面上给出的示例:

>>> import requests
>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
u'{"type":"User"...'
>>> r.json()
{u'disk_usage': 368627, u'private_gists': 484, ...}
Run Code Online (Sandbox Code Playgroud)


Kis*_*war 5

假设您使用的是Requests库,则Response对象没有get方法。

给出的链接解释了Response对象的属性和方法。

如果您想读取响应,您应该查看contentjsontext 的实际数据。