Python YouTube Gdata播放器错误

时间:2010-10-15 00:53:15

标签: python youtube gdata

我正在尝试一些非常简单的事情:从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'

我不知道为什么会发生这种情况,它在几周前发生了...... 有什么想法吗?

1 个答案:

答案 0 :(得分:1)

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

...
for entry in feed.entry:
  if entry.media.player is not None:
    urls.append(entry.media.player.url)
...