Discord.py 谷歌图片搜索

Posted

技术标签:

【中文标题】Discord.py 谷歌图片搜索【英文标题】:Discord.py google images search 【发布时间】:2020-07-18 02:05:41 【问题描述】:

我目前正在尝试制作一个不和谐机器人,它将根据特定命令从谷歌图像发送图像,所以如果我说/dog 它会从谷歌图像中随机发布一张狗的照片。我是创建不和谐机器人的新手,我打算用 Python 制作它。如果有人帮助我提供示例代码,让我了解如何从谷歌图片中获取图片,我将不胜感激。

【问题讨论】:

【参考方案1】:

您可以使用 aiohttp 客户端会话通过 some-random-api 完成此操作

对于示例代码,它应该看起来像这样

    @commands.command(name='dog')
    async def random_dog(self, ctx):
        async with aiohttp.ClientSession() as cs:
            async with cs.get("https://random.dog/woof.json") as r:
                
                data = await r.json()
                embed = discord.Embed(
                    title="Doggo",
                    color = ctx.author.color
                )
                embed.set_image(url=data['url'])
                await ctx.send(embed=embed)

【讨论】:

以上是关于Discord.py 谷歌图片搜索的主要内容,如果未能解决你的问题,请参考以下文章