0

我正在尝试一些非常简单的事情:从 youtube gdata 模块获取播放器网址列表,

def getlist():
  index = 1
  prev = 0
  urls = []
  while True:
    uri = "http://gdata.youtube.com/feeds/api/playlists/E005D335B57338D1?start-index=%i&max-results=50" % index
    feed = yt_service.GetYouTubeVideoFeed(uri)
    for entry in feed.entry:
      urls.append(entry.media.player.url)
    if prev == len(urls): break
    prev = len(urls)
    index += 50
  return urls

但是调用getlist时会出现如下错误:

>>> urls = getlist()
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    getlist()
  File "<pyshell#1>", line 9, in getlist
    urls.append(entry.media.player.url)
AttributeError: 'NoneType' object has no attribute 'url'

我不知道为什么会这样,它在几周前就起作用了……有什么想法吗?

4

1 回答 1

1

您应该添加一个检查以保证播放器不为空:

...
for entry in feed.entry:
  if entry.media.player is not None:
    urls.append(entry.media.player.url)
...
于 2010-11-11T13:11:46.970 回答