Last.fm 顶级艺术家 Api

Last.fm Top Artists Api

  @commands.command()
  async def np(self,ctx):
        async with aiohttp.ClientSession() as session:
            params= {"api_key" : "censored",
            "user" : "ssj4abd",
            "period" : "overall",
             "limit" : 10,
             "method":"user.getTopArtists",
             "format":"json"}
            async with session.get(url="http://ws.audioscrobbler.com/2.0", params=params) as response:
                resp = await response.read()
                print(resp)

我正在这样做,以便它检索用户的顶级(第一)艺术家,回复非常长,您可以找到 here。我如何才能 retrieve/fetch 从所有这些混乱中仅 "rank" : 1 艺术家?

您正在请求 JSON 回复

"format":"json"}

这就是你得到的。要将其加载到字典中,请使用 json

import json
jsonData = json.loads(resp)

现在,您可以通过

获取第一个艺术家的字典
topArtist = jsonData["topartists"]["artist"][0]

从那里,您可以检索所有信息,例如 url

topArtistUrl = topArtist["url"]

import json
@commands.command()
  async def np(self,ctx):
        async with aiohttp.ClientSession() as session:
            params= {"api_key" : "censored",
            "user" : "ssj4abd",
            "period" : "overall",
             "limit" : 10,
             "method":"user.getTopArtists",
             "format":"json"}
            async with session.get(url="http://ws.audioscrobbler.com/2.0", params=params) as response:
                resp = await response.read()
                jsonData = json.loads(resp)
                topArtist = jsonData["topartists"]["artist"][0]
                topArtistUrl = topArtist["url"]