【问题标题】:How to search for track based on track name and artist in spotipy如何在 spotipy 中根据曲目名称和艺术家搜索曲目
【发布时间】:2020-08-12 08:08:39
【问题描述】:

我需要根据歌曲名称和艺术家姓名为用户检索一首歌曲(所以我得到了正确的歌曲)。最终目标是获取track id。 这是我认为可行的代码:

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

artist 是艺术家/乐队的名称,而 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
}

}

【问题讨论】:

    标签: python spotify spotipy


    【解决方案1】:

    您在艺术家和track: 标签之间缺少一个空格。

    之前

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

    之后

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

    【讨论】:

      【解决方案2】:

      我不知道这是否有任何帮助,但在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:' + title + ' artist:' + 艺术家形式的查询不起作用
      【解决方案3】:

      我建议尝试使用 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 表示调用成功。希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2017-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-04
        相关资源
        最近更新 更多