【问题标题】:Extracting element from API从 API 中提取元素
【发布时间】:2021-11-10 09:14:58
【问题描述】:

如何从 API 中提取元素 .. API 看起来像

{"response_code":0,"results":[{"category":"General Knowledge","type":"multiple","difficulty":"hard","question":"Electronic music producer Kygo's popularity skyrocketed after a certain remix. Which song did he remix?","correct_answer":"Ed Sheeran - I See Fire","incorrect_answers":["Marvin Gaye - Sexual Healing","Coldplay - Midnight","a-ha - Take On Me"]}]}

    async with aiohttp.ClientSession() as session:
     request = await session.get('https://opentdb.com/api.php?amount=1&category=9&difficulty=hard')
     questionjson = await request.json()
     request2 = await session.get('https://opentdb.com/api.php?amount=1&category=9&difficulty=hard')
     
     questionjson1 = str(questionjson['results'])
     
    
     embed = discord.Embed(title="**__Trivia Time !!__**",description = questionjson1['question'] ,color=  random.choice(colors))```

And I only want to extract the “question” element from that api  

【问题讨论】:

  • 您能否展示一些代码或告诉我们您尝试了什么?

标签: json api discord.py


【解决方案1】:

所以我使用了 API,然后你就可以了:-

@bot.command()
async def trivia(ctx):
  async with aiohttp.ClientSession() as cs:
    async with cs.get('https://opentdb.com/api.php?amount=1&category=9&difficulty=hard') as r:
      res = await r.json()
      print(res)
      question = res['results'][0]['question'] #This line gets the question
      embed = discord.Embed(title="**__Trivia Time !!__**",description = question ,color=ctx.author.color)
      await ctx.send(embed=embed)

【讨论】:

  • @AtikshKundu 请接受答案,以便人们知道这个问题已得到回答(:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多