【问题标题】:Spotipy - list index out of rangeSpotipy - 列表索引超出范围
【发布时间】:2016-09-25 15:05:04
【问题描述】:

编写 Spotipy 脚本以返回给定专辑中的专辑曲目我偶尔会遇到错误:

    album_id = results["albums"]["items"][0]["uri"]
IndexError: list index out of range

这个错误往往发生在更受欢迎的艺术家循环浏览他们所有专辑的情况下。我猜结果列表要么达到了极限,要么以某种方式失去了顺序。无论哪种方式,我都不确定如何修复它,因为我很确定我从 Sptipy 网站上获得了 album_id 行。有什么想法吗?

#!/usr/bin/python
# -*- coding: utf-8 -*-

import spotipy

sp = spotipy.Spotify()
sp.trace = False


def get_artist_albums(artist):
  results = sp.search(q = "artist:" + artist, type = "artist")
  items = results["artists"]["items"]
  artist = items[0]
  # print artist

  albums = []
  albumsTitles  = []
  results = sp.artist_albums(artist["id"], album_type = "album")
  albums.extend(results["items"])
  while results["next"]:
    results = sp.next(results)
    albums.extend(results["items"])
  seen = set() # to avoid dups
  albums.sort(key = lambda album:album["name"].lower())

  for album in albums:
    albumTitle = album["name"]
    if albumTitle not in seen:
      print((" " + albumTitle))
      seen.add(albumTitle)
      albumsTitles.append(albumTitle)

  # return albumsTitles
  return albumsTitles 

def get_albums_tracks(album):
  albumtracks = []

  results = sp.search(q = "album:" + album, type = "album")

  # get the first album uri
  album_id = results["albums"]["items"][0]["uri"]

  # get album tracks
  tracks = sp.album_tracks(album_id)
  c = 1
  # print album
  for track in tracks["items"]:
    # print "#%s %s" %(c, track["name"])
    albumtracks.append(track["name"])
    c +=1
  return albumtracks


# 3 album band - ok
phosgoreAlbums = get_artist_albums("Phosgore")
for item in phosgoreAlbums:
  print get_albums_tracks(item)
  print ""

# 6 album band - ok
# (well technically 2, but's let not get into that night now)
joyDivisionAlbums = get_artist_albums("Joy Division")
for item in joyDivisionAlbums:
  print get_albums_tracks(item)
  print ""

# 34 albums - falls over
cherAlbums = get_artist_albums("Cher")
for item in cherAlbums:
  print get_albums_tracks(item)
  print ""

# 38 album band - falls over
theCureAlbums = get_artist_albums("The Cure")
for item in theCureAlbums:
  print get_albums_tracks(item)
  print ""

# 43 album band - falls over
aliceCooperAlbums = get_artist_albums("Alice Cooper")
for item in aliceCooperAlbums:
  print get_albums_tracks(item)
  print ""

【问题讨论】:

  • 您能否提供一个失败的艺术家+专辑组合示例?或者,它是不确定的?
  • @alecxe 我用一些统计数据更新了代码。我似乎确实有点随机 - 这没有多大帮助
  • 如果在请求之间添加中间人为延迟会怎样 - 是否有助于降低失败率?..
  • @alecxe 减慢计数 (time.sleep(1)) 没有效果
  • @Martijn Pieters♦ 效果很好,谢谢! Spotify 拥有 Alice Copper,《死亡剧场:2009 年在 Hammersmith 现场直播》中没有音轨——谁知道呢?糟糕的 Spotify,没有饼干。

标签: python list python-2.7 spotipy


【解决方案1】:

results["albums"]["items"] 只是一个空列表,因此索引0 处没有元素。您可以在尝试对其进行索引之前对其进行测试:

if not results["albums"]["items"]:
    # no albums found, so no tracks either
    return []

album_id = results["albums"]["items"][0]["uri"]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-16
    • 2011-06-14
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多