我如何从API获得纯文本?



我很抱歉只是这样问,但我是JSON和api相关的东西的新手。

@bot.command()
async def insult(ctx):
resp = requests.get("https://insult.mattbas.org/api/insult")
if 300 > resp.status_code >= 200:
content = resp.json()  #We have a dict now.
else:
content = f"Recieved a bad status code of {resp.status_code}."
await ctx.send(content)

API显示纯文本,我不知道如何得到它。然而,我可以得到其他没有纯文本的API结果。

请帮助。

您应该调用resp。内容或回应。文本代替json().

@bot.command()
async def insult(ctx):
resp = requests.get("https://insult.mattbas.org/api/insult")
if 300 > resp.status_code >= 200:
content = resp.content  #We have a dict now.
else:
content = f"Recieved a bad status code of {resp.status_code}."
await ctx.send(content)

最新更新