python'InlineResponse200'对象不可订阅

时间:2018-03-10 17:39:48

标签: python object dictionary giphy

import time
import giphy_client
from giphy_client.rest import ApiException
from pprint import pprint

# create an instance of the API class
api_instance = giphy_client.DefaultApi()
api_key = 'dc6zaTOxFJmzC' # str | Giphy API Key.
q = 'cheeseburgers' # str | Search query term or prhase.
limit = 1 # int | The maximum number of records to return. (optional) (default to 25)
offset = 0 # int | An optional results offset. Defaults to 0. (optional) (default to 0)
rating = 'g' # str | Filters results by specified rating. (optional)
lang = 'en' # str | Specify default country for regional content; use a 2-letter ISO 639-1 country code. See list of supported languages <a href = \"../language-support\">here</a>. (optional)
fmt = 'json' # str | Used to indicate the expected response format. Default is Json. (optional) (default to json)

try: 
    # Search Endpoint
    api_response = api_instance.gifs_search_get(api_key, q, limit=limit, offset=offset, lang=lang, fmt=fmt)
    #pprint(api_response)
except ApiException as e:
    print("Exception when calling DefaultApi->gifs_search_get: %s\n" % e)

api_response['data']

您好,我无法弄清楚如何将api_response(内联响应)转换为我可以操作的内容,例如python字典。我需要想办法保存或转换它。我只能打印它。

谢谢!

enter image description here

2 个答案:

答案 0 :(得分:0)

我想出了一种提取数据的方法

x = api_response.data
y = x[0]
y.images.downsized.url

不确定它是否是最好的方法,但它适用于我!

答案 1 :(得分:0)

我来到这里是因为我看到这是使用自动生成的客户端。

https://editor.swagger.io/

修复了.to_dict(),在这里也可以使用。