是否有一种方法可以获取使用Pixabay搜索并使用discord.py发送的图像?

2024-06-10 17:52:13 发布

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

我正在使用Pixabay API搜索图像,然后我想让我的discord.py机器人说出来。但是,我不知道你必须调用什么函数才能获得图像,这样discord机器人才能以嵌入方式发送图像。 这是我目前的代码

@bot.command(name='otter', help='Generates a random otter!')
async def otter(ctx):
    r = requests.get('https://pixabay.com/api/?key=keyisherebutforprivacyreasonsiamremovingitlol&q=otter&image_type=photo')
    r = r.json()
    for item in r['hits']:
        print(item['type'])
        embed = discord.Embed(title='Otter test', color=discord.Color.from_rgb(226, 22, 31))
        embed.set_image(url=item[random.choice(range(0, 5))]) # The problem is here, as I took the image bit away and the embed sent fine.
        embed.set_footer(text='Powered by pixabay.')


    await ctx.send(embed=embed)

基本上,它只是反复打印:照片,这很好,因为这意味着搜索工作正常。作为测试,我删除了set图像部分,嵌入发送正常,因此我知道embed.set_图像有问题。如果有人知道获取并发送图像的正确方法,我将非常感激。谢谢我在控制台中也没有收到错误消息


Tags: the图像imageapitype机器人randomembed
1条回答
网友
1楼 · 发布于 2024-06-10 17:52:13

确保您从响应(r)中获取了正确的内容。如果遇到任何404错误,可以检查url是否包含图像扩展名(.jpg.png,等等)

为了便于将来参考,为了方便地可视化JSON,您可以使用this网站

祝水獭好运

相关问题 更多 >