无法从 discord.py 中的男高音 api 获取 gif URL

cant get gif URL from tenor api in discord.py

我是 python 的新手,不知道该怎么做。

async def get_gif(searchTerm) -> str:  
async with aiohttp.ClientSession() as session:
    l = 50
    response = await session.get("https://api.tenor.com/v1/search?q={}&key={}&limit={}&media_filter=basic".format(searchTerm, KEY, l))
    ri = random.randint(0, l-1)
    data = json.loads(await response.text())
    gif_choice:str = data['results'][ri]['gif']['url']
    session.close()
    return gif_choice

这就是我的功能 rn。这是堆栈跟踪:

Ignoring exception in on_message Traceback (most recent call last): File "C:\Users\Lasse\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 343, in _run_event await coro(*args, **kwargs) File "d:\dev\Samsara bot\Bot.py", line 133, in on_message g:str = await get_gif("koneko") File "d:\dev\Samsara bot\Bot.py", line 76, in get_gif gif_choice:str = data['results'][ri]['gif']['url'] KeyError: 'gif'

这是我的做法。每个结果都有一个 media 键,该键有一个列表,其中包含一个对象,该对象包含不同大小的对象。您可以从他们那里访问他们的网址。我用 'mediumgif' 作为我的。您可以访问不同的格式和大小,例如 'tinygif'、'nanogif' 或 mp4。

data['results'])[ri]['media'][0]['mediumgif']['url']