使用Spoonacular API生成随机食品配方的Discord Bot
我正在尝试在NextCord Python中制作一个Discord Bot,该机器人将使用$ 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'
I am trying to make a discord bot in Nextcord Python that will generate a random recipe using a $randomrecipe command.
I tried making it so that it would send a title of a dish (and also the image and ingredients) but I cant get it to work.
@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)
here's the error for that code
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此错误意味着API未返回标题。
问题在于以下行:
title_name = r.json()[“ title”]
尝试在没有[“ title”]
print(r.json())
的情况下打印put r.json()您会注意到,该标题密钥不包括在响应中。
您正在寻找根部元素中的标题,但是在复制它之后,打印了整个响应,我注意到标题位于食谱中。有乘数标题键,因为您想要菜的标题(这是第一个),您需要按0索引(计算机开始在0),因此您应该尝试使用
r.json()[)[ “食谱”] [0] [“标题”]
使用这种方式后,我得到了输出的输出:
整天简单的慢煮者从骨肋骨上掉下来
这里我使用的完整代码:
This error means, that the api not returned a title.
The issue lies in following line:
title_name = r.json()["title"]
Try to print put r.json() without the ["title"]
print(r.json())
You will notice, that title key is not included in the response.
You are looking for title in the root element but after reproducing it, printing out the whole response, i noticed that title is located inside recipes. There are multiplie title keys and because you want the title of the dish (which is the first one) you need to index it by 0 (computers start couting at 0) so you should try to use
r.json()["recipes"][0]["title"]
After using it this way, i got follwoing output:
All Day Simple Slow-Cooker FALL OFF the BONE Ribs
Here the full code i've used: