python - 如何从 API 获取纯文本?

标签 python discord.py

很抱歉这么问,但我对 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.content 或 resp.text 而不是 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)

关于python - 如何从 API 获取纯文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73030173/

相关文章:

python - 将命名空间用于数据存储或将命名空间用于内存缓存

python - 第一次完成后取消两个异步协程

discord.py - 我经常收到 HTTP 错误 403 Forbidden using discord.py and ytdl

python - Discord 机器人阅读 react

Python ffmpeg 在 heroku 上不起作用,没有错误,但它只是在那一行卡住

javascript - 为什么我无法从 POST 请求中获取文件?

python - 我可以让 mean_iou 依赖于 update_op 吗?它们都由 tf.metrics.mean_iou() 返回

python - 如何在 Maya 中找到面邻居?

python - 对字符串列表进行排序并在python中的字母后放置数字

python - Discord.py 尝试循环日志并发送到服务器时卡住