【问题标题】:Youtube API Handling Deleted video errorYoutube API 处理删除视频错误
【发布时间】:2013-11-11 14:55:12
【问题描述】:

我已经编写了代码以在不同的文本文件中获取播放列表和其中的视频列表:

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

"""

YouTube Playlist Extrator.
A tool to extract playlists from YouTube API which in todays YouTube page format is very difficult to extract.
It also extracts video lists per playlist and hence takes bit longer to run for long playlists.

"""


#from profiler import Profiler
from xml.dom.minidom import parseString
import os

try:
    import urllib.request as urlLibReq
    PY3 = True
except:
    import urllib as urlLibReq
    PY3 = False

def getInput():
    if PY3:
        return input("Enter username of YouTube channel: ")
    elif not PY3:
        return raw_input("Enter username of YouTube channel: ")

def xmlParser(url):
  page = urlLibReq.urlopen(url)
  text = page.read().decode("utf8")
  return parseString(text)

def extractplaylist(userId):
    url = "https://gdata.youtube.com/feeds/api/users/"+ userId +"/playlists?v=2"
    dom = xmlParser(url)
    total = int(dom.getElementsByTagName("openSearch:totalResults")[0].firstChild.nodeValue)
    startIndex, listEntry = 1 , []
    while startIndex <= total:
        url_new = url + "&max-results=50&start-index="+ str(startIndex)
        dom = xmlParser(url_new)
        entry = dom.getElementsByTagName("entry")
        for node in entry:
            id_data = node.getElementsByTagName("id")[0].firstChild.nodeValue
            id_split = id_data.split(':')
            playlist_id = id_split[5]
            playlist_title = node.getElementsByTagName("title")[0].firstChild.nodeValue
            extractvideolist(userId, playlist_id, playlist_title)
            listEntry.append(str(playlist_title))
            startIndex += 1
    listEntry.sort()
    writer = open(userId+"_playlist.txt","w")
    writer.write("\r\n".join(map(str, listEntry)))
    writer.close()

def extractvideolist(userId, playlist_id, playlist_title):
    url = "http://gdata.youtube.com/feeds/api/playlists/"+ playlist_id +"?v=2"
    dom = xmlParser(url)
    total = int(dom.getElementsByTagName("openSearch:totalResults")[0].firstChild.nodeValue)
    startIndex, listEntry = 1 , []
    while startIndex <= total:
        url_new = url + "&max-results=50&start-index="+ str(startIndex)
        dom = xmlParser(url_new)
        entry = dom.getElementsByTagName("entry")
        for node in entry:
            video_title = node.getElementsByTagName("title")[0].firstChild.nodeValue
            listEntry.append(str(video_title))
            startIndex += 1
    playlist_title = playlist_title.replace("'","\'")
    writer = open(playlist_title+"_videolist.txt","w")
    writer.write("\r\n".join(map(str, listEntry)))
    writer.close()
    print("written", playlist_title)
    try: os.mkdir(userId)
    except: pass
    os.system('mv "'+ playlist_title +'_videolist.txt" '+ userId)


if __name__ == "__main__":
    name = getInput()
    extractplaylist(name)
    #Profiler.report()

当播放列表中有删除的视频时,代码会失败。遇到这种情况怎么办?

【问题讨论】:

  • 如果您能提供此失败的用例、代码中的哪一行失败和/或您看到的错误,将会有所帮助。
  • 我主要需要来自用户 ['nptelhrd'][1] 的数据。播放列表播放列表 Chemical - Mass Transfer Operations I 有一个已删除的视频,因此在第 66 到 69 行失败。[1]youtube.com/user/nptelhrd

标签: python python-3.x youtube-api


【解决方案1】:

尝试在 for 循环中添加 else 子句,以便在 for 循环结束时跳出 while 循环。

while startIndex <= total:
    url_new = url + "&max-results=50&start-index="+ str(startIndex)
    dom = xmlParser(url_new)
    entry = dom.getElementsByTagName("entry")
    for node in entry:
        id_data = node.getElementsByTagName("id")[0].firstChild.nodeValue
        id_split = id_data.split(':')
        playlist_id = id_split[5]
        playlist_title = node.getElementsByTagName("title")[0].firstChild.nodeValue
        extractvideolist(userId, playlist_id, playlist_title)
        listEntry.append(str(playlist_title))
        startIndex += 1
    else:
        break

【讨论】:

  • 有时这些基本的东西不会在编码瞬间发生。谢谢
猜你喜欢
  • 2012-01-02
  • 2018-11-30
  • 2021-10-12
  • 2013-02-16
  • 2020-10-24
  • 2020-11-17
  • 2015-02-16
  • 2016-08-25
  • 1970-01-01
相关资源
最近更新 更多