“InlineResponse2002”对象不可下标|如何操作API响应?

2024-05-18 16:17:43 发布

您现在位置:Python中文网/ 问答频道 /正文

代码:

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

def giphyapi():
    api_instance = giphy_client.DefaultApi()
    api_key = '################################'
    tag = 'test'
    rating = 'pg-13'
    fmt = 'json'

    try:
        # Search Endpoint
        api_response = api_instance.gifs_random_get(api_key, tag = tag, rating = rating, fmt = fmt)
        ## here’s where I want to do stuff with the data
    except ApiException as exc:
        print("Exception when calling DefaultApi->gifs_random_get: %s\n" % exc)
    return None

giphyapi()

嗨!如何将api_instance转换成可操作的东西,例如dict?
这与this是同一个问题,但作者遗憾地发现,这个问题的解决方案对我不起作用

我已经尝试了print(api_response.data[0].images.type),但这引发了以下错误:
TypeError:“RandomGif”对象不可下标

我也试过:

for block in api_response["data"]:
    giftype = block["type"]

但这引发了这个错误: TypeError:“InlineResponse2002”对象不可下标

我使用的是Python 3.8.1,我还使用了giphy-python-clientHereRandomGif模型的列表。我在上面尝试的两个例子中试图获取的是type

非常感谢您的帮助!🙂


Tags: instancefromimportclientapidataresponsetag

热门问题