首页 > 解决方案 > subpods.img 的返回类型

问题描述

我希望我的代码查询 wolfram 并获取它返回的图像。但是 wolfram API 的返回类型不符合我的要求。

subpods.img 返回一个类映射对象。Discord 只能发送图像文件。当返回类型不兼容时,我该怎么做。我的代码如下所示。

当我实际尝试打印 pod.img 的类型时,它表明它实际上来自类映射。我应该在这里做什么?

async def printImgPod(ctx, img, title):
    newmessage = await ctx.send("\__\**" + title + ":\**__\n" + "`" ,file= img)
    messageHistory.add(newmessage)
@bot.command()
< all the rest of the code for the command >
res = waclient.query(query)
if len(list(res.pods)) > 0:
   for pod in res.pods:
        if pod.text:
            <code for printing text in pod>
        else:
            for sub in pod.subpods:
                if sub.img:
                    await printImgPod(ctx.message.channel, sub.img, pod.title)

标签: pythonpython-3.xwolframalpha

解决方案


它看起来像是sub.img一个字典的迭代器。要获取图像的 URL,请尝试

for sub in pod.subpods:
    for img in sub.img:
        await printImgPod(ctx.message.channel, img['@src'], pod.title)

推荐阅读