Youtube API处理删除的视频

2024-04-25 21:40:00 发布

您现在位置:Python中文网/ 问答频道 /正文

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

#!/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()

当播放列表中有已删除的视频时,代码失败。我怎么处理这种事?在


Tags: importidurltitleyoutubeplaylistdomwriter
1条回答
网友
1楼 · 发布于 2024-04-25 21:40:00

尝试在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

相关问题 更多 >