【问题标题】:spotify search API queryspotify 搜索 API 查询
【发布时间】:2018-09-09 16:25:41
【问题描述】:

我如何从下面的代码中获取关注者数量和流派以及其他结果:

q="A"
sp.search(q,limit=1,offset=0,type="artist",market="US")
followers=result['artists']['items']
pprint(followers)

[{'external_urls': {'spotify': 
'https://open.spotify.com/artist/0ZXKT0FCsLWkSLCjoBJgBX'},
'followers': {'href': None, 'total': 121213},
'genres': ['gothic metal', 'progressive metal'],
'href': 'https://api.spotify.com/v1/artists/0ZXKT0FCsLWkSLCjoBJgBX',
'id': '0ZXKT0FCsLWkSLCjoBJgBX',
'images': [{'height': 640,
          'url': 
'https://i.scdn.co/image/350538b94d7d1829d983eaf4ee4aeca2af071f2c',
          'width': 640},
         {'height': 320,
          'url': 
'https://i.scdn.co/image/1a00c2a831a77f681516d3be11f2f0c6a9bb85b2',
          'width': 320},
         {'height': 160,
          'url': 
'https://i.scdn.co/image/8066cb28d68d3ce4924f33e6eca1b3a2a391eca6',
          'width': 160}],
'name': 'Anathema',
'popularity': 53,
'type': 'artist',
'uri': 'spotify:artist:0ZXKT0FCsLWkSLCjoBJgBX'}]

【问题讨论】:

    标签: python spotify spotipy


    【解决方案1】:

    对于初学者,我建议通过访问以下链接来复习 Python 中的数据结构:https://docs.python.org/3/tutorial/datastructures.html
    但为了更直接地回答您的问题,您在此处定义的“关注者”列表似乎是字典项目列表。在您的情况下,列表的长度仅为 1,因此您可以按如下方式访问其中的内容:

    contents = followers[0]  
    

    (注意:如果列表的长度大于 1,这只会返回列表中的第一个字典项。如果长度大于 1,则需要遍历列表以查找所有元素。)
    这将返回与第一个也是唯一一个关注者元素关联的字典项。从那里,您可以通过执行以下操作访问字典的特定部分:

    contents['genre']
    

    contents['followers'] 
    

    (或对字典中其他数据字段的类似调用)
    现在有了第一个,引用“流派”类型,您将得到一个列表。此列表包含该特定艺术家所属的所有不同类型。在这种情况下,Anathema(返回乐队的名称)既属于哥特金属,也属于前卫金属。因此,通过访问 contents['genre'] 返回的列表将只包含这两个元素。
    此外,contents['followers'] 将返回另一个字典:这个字典包括与 Anathema 乐队的追随者有关的信息。要访问关注者数量,您可以执行以下操作:

    follow_count = contents['followers']['total']
    

    这应该会为您提供您正在寻找的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 2018-10-27
      • 2018-06-12
      • 2015-11-13
      • 1970-01-01
      • 1970-01-01
      • 2018-09-30
      相关资源
      最近更新 更多