You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何无需手动复制Spotify艺术家URI,通过代码获取且不使用Selenium

无需Selenium获取Spotify艺术家URI的实现方法

Spotify官方提供了原生搜索API,可直接通过代码自动获取艺术家URI,无需手动操作,实现逻辑如下:

  1. 注册Spotify开发者账号,创建应用获取Client ID和Client Secret
  2. 通过客户端凭证模式请求接口获取访问令牌(access token)
  3. 调用搜索接口,传入艺术家名称作为搜索关键词,指定搜索类型为artist,返回结果中匹配度最高的艺术家条目下的id字段,加上spotify:artist:前缀就是所需的艺术家URI

以下是两种实现代码示例:

  1. 原生requests实现:
import requests

# 替换为自己的应用凭证
CLIENT_ID = "你的Client ID"
CLIENT_SECRET = "你的Client Secret"

# 获取访问令牌
auth_url = "https://accounts.spotify.com/api/token"
auth_response = requests.post(auth_url, {
    'grant_type': 'client_credentials',
    'client_id': CLIENT_ID,
    'client_secret': CLIENT_SECRET,
})
access_token = auth_response.json()['access_token']
headers = {'Authorization': f'Bearer {access_token}'}

# 搜索艺术家获取URI
search_url = "https://api.spotify.com/v1/search"
params = {
    'q': '目标艺术家名称',
    'type': 'artist',
    'limit': 1 # 仅返回匹配度最高的1个结果
}
response = requests.get(search_url, headers=headers, params=params)
artist_data = response.json()['artists']['items'][0]
artist_uri = artist_data['uri'] # 直接获取完整艺术家URI
artist_cover = artist_data['images'][0]['url'] # 同时可获取艺术家封面
print(f"艺术家URI:{artist_uri}")
  1. 封装库spotipy实现,代码更简洁:
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(
    client_id="你的Client ID",
    client_secret="你的Client Secret"
))
result = sp.search(q='目标艺术家名称', type='artist', limit=1)
artist_uri = result['artists']['items'][0]['uri']

可调整limit参数返回多个匹配结果,结合粉丝数、专辑数量等字段筛选,进一步提高匹配准确率。

适合Python的音乐元数据+串流替代方案
  • Spotify API默认仅返回30秒音乐预览片段,若需要完整串流功能,需要申请对应权限且要求用户登录Spotify Premium账号。如果不需要绑定Spotify生态,可以选择Deezer公开API,无需复杂的权限申请,调用搜索接口即可直接获取音乐元数据、封面和30秒预览串流地址,适配Python请求非常方便。
  • 如果项目可以接受用户登录对应音乐平台账号,可以使用YouTube Music的Python封装库,资源覆盖量更广,串流稳定性更好。
  • 所有公开音乐API的串流服务都受对应平台版权协议约束,商用前请仔细核对相关条款,避免侵权风险。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 05:15:02