如何在没有json的情况下获得python中的API输出

2024-09-30 22:22:14 发布

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

这是我正在尝试的apihttps://api.apithis.net/host2ip.php?hostname=google.com 正如您所看到的,与json API不同,它没有可以复制为response.json的“”

@client.command()
async def host(ctx, host):
    url = ('https://api.apithis.net/host2ip.php?hostname=' + host)

    response = requests.get(url)
    ipaddress = response.json()

    embed = discord.Embed(title="IP for website" + host, color=0x00ffff)
    embed.add_field(name="IP:", value=f'{ipaddress}', inline=True)                                                            
    
    await ctx.send(embed=embed)

这是我目前的代码,如果有人可以修复,这会有很大帮助,因为我不知道如何在没有json输出的情况下使用API,谢谢


Tags: ipapijsonhosturlnetresponseembed
2条回答

首先,您得到的响应不是JSON。如果您看到response header,那么您可以看到它是 content-type: text/html; charset=UTF-8

既然它不是JSON,就必须将其视为文本

r = requests.get(url)
ipaddress = r.text if r.status_code == 200 else ''

您可以简单地使用Response.text属性

reponse = requests.get(url).text # e.g 0.0.0.0

相关问题 更多 >