在Python中检索Spotify播放列表关注者计数

问题描述

我想使用Python检索我的Spotify播放列表的关注者计数。我一直在搜索https://developer.spotify.com/documentation/web-api/reference-beta/#category-playlists,但还没有找到一种方法。但是,我找到了一个有效的代码,关于如何检索播放列表的曲目ID,如何调整它以获取关注者呢?

def getTrackIDs(user,playlist_id):
    ids = []
    playlist = sp.user_playlist(user,playlist_id)
    for item in playlist['tracks']['items']:
        track = item['track']
        ids.append(track['id'])
    return ids

ids = getTrackIDs('User','Playlist_Id')

print(len(ids))
print(ids)

解决方法

看看应该通过获取播放列表返回的播放列表对象。

https://developer.spotify.com/documentation/web-api/reference/object-model/#playlist-object-full

它具有followers属性,该属性是位于此处的关注者对象

https://developer.spotify.com/documentation/web-api/reference/object-model/#followers-object

关注者对象包含total属性,应该是您要寻找的属性。

尽管我无法运行您的代码,但我认为结果应该如下所示。让我知道这是否对您有用(我无法运行代码)。

def getPlaylistFollowerCount(user,playlist_id):
    playlist = sp.user_playlist(user,playlist_id)
    return playlist['followers']['total']