很抱歉这样问,但我对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显示纯文本,我不知道如何获取它.

请帮忙.

推荐答案

你应该给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)

Python相关问答推荐

海运图:调整行和列标签

在Python Attrs包中,如何在field_Transformer函数中添加字段?

pandas滚动和窗口中有效观察的最大数量

Godot:需要碰撞的对象的AdditionerBody2D或Area2D以及queue_free?

avxspan与pandas period_range

DataFrames与NaN的条件乘法

如何在Polars中从列表中的所有 struct 中 Select 字段?

多处理队列在与Forking http.server一起使用时随机跳过项目

在两极中过滤

手动设置seborn/matplotlib散点图连续变量图例中显示的值

Pandas—堆栈多索引头,但不包括第一列

从源代码显示不同的输出(机器学习)(Python)

如果有2个或3个,则从pandas列中删除空格

语法错误:文档. evaluate:表达式不是合法表达式

用fft计算指数复和代替求和来模拟衍射?

使用SQLAlchemy从多线程Python应用程序在postgr中插入多行的最佳方法是什么?'

TypeError:';Locator';对象无法在PlayWriter中使用.first()调用

将数字数组添加到Pandas DataFrame的单元格依赖于初始化

如何在Pandas中用迭代器求一个序列的平均值?

在Django REST框架中定义的URL获得404分