You need to enable JavaScript to run this app. 使用Spotipy开发时如何获取歌曲的全部艺人列表?
优惠活动
大模型
产品
解决方案
定价
更多

使用Spotipy开发时如何获取歌曲的全部艺人列表?

修复Spotipy搜索仅获取首位艺人的问题

Spotify返回的搜索结果中,searched['tracks']['items'][0]['artists']本身就是包含目标曲目全部参与艺人的列表,原代码硬写的[0]索引只会截取列表第一个元素,丢失其他合作艺人的数据。

基础实现

核心逻辑为遍历整个artists列表提取所有艺人名,适配Discord bot的展示需求:

# 提取单条搜索结果公共对象,减少重复长路径写法
target_track = searched['tracks']['items'][0]
# 批量提取所有艺人名称
all_artist_names = [artist['name'] for artist in target_track['artists']]
# 拼接为易读的展示字符串,分隔符可根据需求自行调整
artist_display = "、".join(all_artist_names)

异常兜底优化

为避免接口异常返回空数组导致的报错,可增加简单判断逻辑:

target_track = searched['tracks']['items'][0]
if not target_track['artists']:
    artist_display = "未知艺人"
else:
    all_artist_names = [artist['name'] for artist in target_track['artists']]
    artist_display = "、".join(all_artist_names)

补充说明:如果后续需要提取单个艺人的ID、主页链接等其他属性,同样可以在遍历target_track['artists']的循环中批量获取,无需硬取第一位元素。


内容的提问来源于stack exchange,提问作者Tri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.13 16:24:02