使用 Spoonacular 生成随机食物食谱的 Discord Bot api
Discord Bot that generates random food recipe's using Spoonacular api
我正在尝试在 Nextcord Python 中制作一个 discord 机器人,它将使用 $randomrecipe 命令生成随机食谱。
我试着让它发送一道菜的标题(还有图片和配料),但我无法让它工作。
@bot.command(name='randomrecipe')
async def randomrecipe(ctx):
r = requests.get('https://api.spoonacular.com/recipes/random?apiKey=apikey')
title_name = r.json() ["title"]
await ctx.send(title_name)
这是该代码的错误
Ignoring exception in command randomrecipe:
Traceback (most recent call last):
File "C:\Users\iliaa\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\core.py", line 168, in wrapped
ret = await coro(*args, **kwargs)
File "c:\Users\iliaa\Downloads\json\app.py", line 22, in randomrecipe
title_name = r.json() ["title"]
KeyError: 'title'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\iliaa\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\bot.py", line 1048, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\iliaa\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\core.py", line 933, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\iliaa\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\core.py", line 177, in wrapped
raise CommandInvokeError(exc) from exc
nextcord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'title'
此错误表示 api 未返回标题。
问题在于以下行:title_name = r.json()["title"]
尝试在没有 ["title"] print(r.json())
的情况下打印 put r.json()
您会注意到,标题键不包含在响应中。
您正在寻找根元素中的标题,但在复制它、打印出整个响应后,我注意到标题位于食谱中。有多个标题键,因为你想要菜的标题(这是第一个)你需要用 0 索引它(计算机从 0 开始计算)所以你应该尝试使用 r.json()["recipes"][0]["title"]
以这种方式使用后,我得到了以下输出:
All Day Simple Slow-Cooker FALL OFF the BONE Ribs
这里是我使用的完整代码:
import requests
r = requests.get('https://api.spoonacular.com/recipes/random?apiKey=your_key_here')
print(r.json()["recipes"][0]["title"])
我正在尝试在 Nextcord Python 中制作一个 discord 机器人,它将使用 $randomrecipe 命令生成随机食谱。 我试着让它发送一道菜的标题(还有图片和配料),但我无法让它工作。
@bot.command(name='randomrecipe')
async def randomrecipe(ctx):
r = requests.get('https://api.spoonacular.com/recipes/random?apiKey=apikey')
title_name = r.json() ["title"]
await ctx.send(title_name)
这是该代码的错误
Ignoring exception in command randomrecipe:
Traceback (most recent call last):
File "C:\Users\iliaa\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\core.py", line 168, in wrapped
ret = await coro(*args, **kwargs)
File "c:\Users\iliaa\Downloads\json\app.py", line 22, in randomrecipe
title_name = r.json() ["title"]
KeyError: 'title'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\iliaa\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\bot.py", line 1048, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\iliaa\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\core.py", line 933, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\iliaa\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\core.py", line 177, in wrapped
raise CommandInvokeError(exc) from exc
nextcord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'title'
此错误表示 api 未返回标题。
问题在于以下行:title_name = r.json()["title"]
尝试在没有 ["title"] print(r.json())
的情况下打印 put r.json()
您会注意到,标题键不包含在响应中。
您正在寻找根元素中的标题,但在复制它、打印出整个响应后,我注意到标题位于食谱中。有多个标题键,因为你想要菜的标题(这是第一个)你需要用 0 索引它(计算机从 0 开始计算)所以你应该尝试使用 r.json()["recipes"][0]["title"]
以这种方式使用后,我得到了以下输出:
All Day Simple Slow-Cooker FALL OFF the BONE Ribs
这里是我使用的完整代码:
import requests
r = requests.get('https://api.spoonacular.com/recipes/random?apiKey=your_key_here')
print(r.json()["recipes"][0]["title"])