Python - 从 Last.fm API 中检索所有流派标签

Python - retrieve all genre tags from Last.fm API

我有一个函数使用 get_top_tags() 方法来获取 last.fm API 中的顶级标签:

def lastfm_tags(tag):
        tag_weight = {}

        result = last.get_top_tags()

        for tag in result:
            tag_weight[str(tag.item.get_name())] = str(tag.weight) 

        tag_weight = {k: int(v) for k, v in tag_weight.items()}


        return tag_weight

    print lastfm_tags('pop') 

打印:

{'industrial': 533792, 'punk': 849254, 'indie': 1972289, 'metal': 1214142, 'heavy metal': 652768, 'japanese': 428483, 'pop': 1874644, 'new wave': 399642, 'black metal': 772854, 'rap': 513369, 'ambient': 1030860, 'alternative': 2059622, 'hard rock': 821047, 'electronic': 2289385, 'blues': 531227, 'folk': 882684, 'classic rock': 1123889, 'alternative rock': 1123671, '90s': 447817, 'Progressive metal': 407423, 'indie rock': 850647, 'electronica': 614612, 'female vocalists': 1558073, 'Soundtrack': 529623, 'dance': 769325, 'psychedelic': 458873, '80s': 752035, 'piano': 410036, 'chillout': 636262, 'post-rock': 426621, 'punk rock': 518635, 'jazz': 1117396, 'seen live': 2098252, 'instrumental': 818216, 'singer-songwriter': 810484, 'acoustic': 461045, 'hardcore': 656385, 'funk': 399980, 'Classical': 539399, 'Hip-Hop': 814911, 'death metal': 671617, 'soul': 641317, 'british': 667770, 'thrash metal': 465438, 'hip hop': 395139, 'rock': 3879776, 'metalcore': 444620, 'german': 409117, 'Progressive rock': 693750, 'experimental': 1010752}

然后我还可以检索 tags相对于艺术家:

def lastfm_artist_to_tags(artist):

    tag_weight = {}

    result = last.get_artist(artist).get_top_tags()

    for tag in result:
        tag_weight[str(tag.item.get_name())] = str(tag.weight) 

    tag_weight = {k: int(v) for k, v in tag_weight.items()}

    return sorted(tag_weight.items(), key=lambda x: x[1], reverse=True)

打印更具体的标记:

[('shoegaze', 100), ('experimental', 72), ('psychedelic', 70), ('seen live', 61), ('indie', 56), ('indie rock', 53), ('ambient', 14), ('post-rock', 12), ('noise rock', 11), ('dream pop', 9), ('Psychedelic Rock', 8), ('Neo-Psychedelia', 7), ('american', 7), ('rock', 7), ('post-punk', 6), ('Experimental Rock', 4), ('alternative', 4), ('art rock', 4), ('noise', 4), ('atlanta', 4), ('00s', 3), ('alternative rock', 3), ('Garage Rock', 3), ('ambient punk', 3), ('georgia', 3), ('noise pop', 3), ('USA', 2), ('Lo-Fi', 2), ('Avant-Garde', 2), ('Dreamy', 2), ('Psychedelia', 2), ('shoegazing', 2), ('psychedelic pop', 2), ('space rock', 2), ('kranky', 2), ('indie pop', 2), ('male vocalists', 1), ('10s', 1), ('deerhunter', 1), ('avantgarde', 1), ('4ad', 1)]

有没有一种我不知道的方法来获取所有可能的标签?

根据LastFM API,不,没有。您只能找到与特定实体(艺术家、专辑、曲目等)关联的标签

这实际上是大多数公司用于他们提供的 API 的常见模式。想象一下,如果您可以免费获得所有标签以及所有相关专辑、艺术家和曲目并将它们存储在本地副本中。您还会回到他们的 API / 网站寻求帮助吗?