Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Youtube API处理删除的视频错误_Python_Python 3.x_Youtube Api - Fatal编程技术网

Python Youtube API处理删除的视频错误

Python Youtube API处理删除的视频错误,python,python-3.x,youtube-api,Python,Python 3.x,Youtube Api,我编写了代码,可以在不同的文本文件中获取播放列表和其中的视频列表: #!/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 lis

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

#!/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()
#/usr/bin/env python
#-*-编码:utf-8-*-
"""
YouTube播放列表外挂程序。
从YouTube API中提取播放列表的工具,在今天的YouTube页面格式中,提取播放列表非常困难。
它还提取每个播放列表的视频列表,因此运行较长的播放列表需要更长的时间。
"""
#从分析器导入分析器
从xml.dom.minidom导入解析字符串
导入操作系统
尝试:
将urllib.request作为urlLibReq导入
PY3=真
除:
将urllib导入为urlLibReq
PY3=假
def getInput():
如果PY3:
返回输入(“输入YouTube频道的用户名:”)
elif非PY3:
返回原始输入(“输入YouTube频道的用户名:”)
def xmlParser(url):
page=urlibreq.urlopen(url)
text=page.read().decode(“utf8”)
返回解析字符串(文本)
def提取播放列表(用户ID):
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尝试向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

虽然startIndex,但如果您能提供此失败的用例,代码中的哪一行失败,和/或您看到的错误,这将有所帮助。我主要需要用户['nptelhrd'][1]提供的数据。playlist playlist Chemical-Mass Transfer Operations I有一个已删除的视频,因此在第66到69行中失败。[1] youtube.com/user/nptelhrd有时这些基本的东西不会在编码的瞬间出现。谢谢