如何在spotipy中根据曲目名称和艺术家搜索曲目

2024-04-28 03:07:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要根据歌曲名称和艺术家名称为用户检索歌曲(以便获得正确的歌曲)。最终目标是获取曲目id。 以下是我认为有效的代码:

searchResults = spotifyObject.search(q="artist:" + artist + "track:" + searchQuery, type="track")

其中,艺术家是艺术家/乐队的名称,searchQuery是曲目的名称。 这将返回特定曲目的JSON块,但会返回:

{
"tracks": {
    "href": "https://api.spotify.com/v1/search?query=artist%3AHarry+Stylestrack%3AWatermelon+Sugar&type=track&offset=0&limit=10",
    "items": [],
    "limit": 10,
    "next": null,
    "offset": 0,
    "previous": null,
    "total": 0
}

}


Tags: 用户名称idsearchartisttypetrack歌曲
3条回答

我不知道这是否有帮助,但在spotipy对函数的定义中,“track”是默认的类型,因此如果您将艺术家和歌曲名称都传递到查询中,您有99%的机会获得您正在搜索的内容。您可以尝试以下方法:

searchQuery = track + ' ' + artist
searchResults = spotifyObject.search(q=searchQuery)

使用track="New Rules"artist="Dua Lipa"得到这个结果(在本例中,我包括了参数market="US"limit=1):

{
  'tracks': {
    'href': 'https://api.spotify.com/v1/search?query=New+Rules+Dua+Lipa&type=track&market=US&offset=0&limit=1',
    'items': [{
      'album': {
        'album_type': 'album',
        'artists': [{
          'external_urls': {
            'spotify': 'https://open.spotify.com/artist/6M2wZ9GZgrQXHCFfjv46we'
          },
          'href': 'https://api.spotify.com/v1/artists/6M2wZ9GZgrQXHCFfjv46we',
          'id': '6M2wZ9GZgrQXHCFfjv46we',
          'name': 'Dua Lipa',
          'type': 'artist',
          'uri': 'spotify:artist:6M2wZ9GZgrQXHCFfjv46we'
        }],
        'external_urls': {
          'spotify': 'https://open.spotify.com/album/01sfgrNbnnPUEyz6GZYlt9'
        },
        'href': 'https://api.spotify.com/v1/albums/01sfgrNbnnPUEyz6GZYlt9',
        'id': '01sfgrNbnnPUEyz6GZYlt9',
        'images': [{
          'height': 640,
          'url': 'https://i.scdn.co/image/ab67616d0000b2736b915e407b70e121e06fe979',
          'width': 640
        }, {
          'height': 300,
          'url': 'https://i.scdn.co/image/ab67616d00001e026b915e407b70e121e06fe979',
          'width': 300
        }, {
          'height': 64,
          'url': 'https://i.scdn.co/image/ab67616d000048516b915e407b70e121e06fe979',
          'width': 64
        }],
        'name': 'Dua Lipa (Deluxe)',
        'release_date': '2017-06-02',
        'release_date_precision': 'day',
        'total_tracks': 17,
        'type': 'album',
        'uri': 'spotify:album:01sfgrNbnnPUEyz6GZYlt9'
      },
      'artists': [{
        'external_urls': {
          'spotify': 'https://open.spotify.com/artist/6M2wZ9GZgrQXHCFfjv46we'
        },
        'href': 'https://api.spotify.com/v1/artists/6M2wZ9GZgrQXHCFfjv46we',
        'id': '6M2wZ9GZgrQXHCFfjv46we',
        'name': 'Dua Lipa',
        'type': 'artist',
        'uri': 'spotify:artist:6M2wZ9GZgrQXHCFfjv46we'
      }],
      'disc_number': 1,
      'duration_ms': 209320,
      'explicit': False,
      'external_ids': {
        'isrc': 'GBAHT1600310'
      },
      'external_urls': {
        'spotify': 'https://open.spotify.com/track/2ekn2ttSfGqwhhate0LSR0'
      },
      'href': 'https://api.spotify.com/v1/tracks/2ekn2ttSfGqwhhate0LSR0',
      'id': '2ekn2ttSfGqwhhate0LSR0',
      'is_local': False,
      'is_playable': True,
      'name': 'New Rules',
      'popularity': 81,
      'preview_url': 'https://p.scdn.co/mp3-preview/75a1b521de23958a2db9acf4fc8151999ee54bd7?cid=aba114e12c4b474895556922ce1a572d',
      'track_number': 10,
      'type': 'track',
      'uri': 'spotify:track:2ekn2ttSfGqwhhate0LSR0'
    }],
    'limit': 1,
    'next': 'https://api.spotify.com/v1/search?query=New+Rules+Dua+Lipa&type=track&market=US&offset=1&limit=1',
    'offset': 0,
    'previous': None,
    'total': 53
  }
}

艺术家和track:标记之间缺少空格

以前

searchResults = spotifyObject.search(q="artist:" + artist + "track:" + searchQuery, type="track")

之后

searchResults = spotifyObject.search(q="artist:" + artist + " track:" + searchQuery, type="track")

我建议尝试使用Spotify提供的API调用链接,查看Spotify开发者(https://developer.spotify.com/documentation/web-api/reference/search/search/)中的以下文档登录帐户后,不要忘记获取访问令牌

import request
import pprint

url = "#link_created_through_spotify_api"
r = request.get(url)
dictionary = r.json() # Turn call info into dictionary
print("Status code:", r.status_code) # Print status code to see if the call was successful.

pprint(dictionary)

pprint模块将允许以非常格式化和有组织的方式打印字典。状态代码为200表示呼叫成功。希望这能有所帮助

相关问题 更多 >