Python 为什么YoutubeDL只提取播放列表的第一个索引?
我正在做一个与播放列表相关的项目,我创建了一个从YoutubeDL继承的类Python 为什么YoutubeDL只提取播放列表的第一个索引?,python,youtube-dl,Python,Youtube Dl,我正在做一个与播放列表相关的项目,我创建了一个从YoutubeDL继承的类 class Playlist(youtube_dl.YoutubeDL): info = {} """ Base class for any playlist. Inherits from youtube_dl.YoutubeDL and overrides some methods to make sure that the link is an actual playlist and
class Playlist(youtube_dl.YoutubeDL):
info = {}
"""
Base class for any playlist. Inherits from youtube_dl.YoutubeDL and overrides some methods to make sure that the
link is an actual playlist and implements some cool stuff to make playlist loading easier.
:var info: the stored information from the extract_info() method
:type info: dict
"""
def extract_info(self, url, download=False, ie_key=None, extra_info=None, process=True, force_generic_extractor=False):
"""
Loads the playlist info and make sures the link is a playlist
:param url: the link with the info to be loaded
:param download: will it download?
:param ie_key: see youtube_dl's doc
:param extra_info: see youtube_dl's doc
:param process: see youtube_dl's doc
:param force_generic_extractor: see youtube_dl's doc
:return: extract_info(...)["entries"] = self.info
:rtype: list
"""
info = super(Playlist, self).extract_info(url, download, ie_key, extra_info, process, force_generic_extractor)
if "_type" not in info:
raise BaseException("\"_type\" not in \"info\"!\n" + info.__str__())
if info["_type"] != "playlist":
raise NotPlaylistError("The url %s does not appear to be a playlist" % url)
self.info = info["entries"]
return self.info
def download(self, url_list=list()):
"""
The inherited download method - now can be used without parameters. If no url_list specified, uses self.info
:param url_list: the url list to be downloaded
:return: nothing
:rtype: None
"""
super(Playlist, self).download(url_list if url_list else self.info)
因此,我只添加了一个带有
playlist().extract\u info(我的url)
的播放列表。但它引发了一个键错误
,这是由“\u type”
键引起的。因此,我玩了一会儿代码,发现代码只下载了整个播放列表的第一个索引(此处发布的实际代码已通过引发BaseError
)来处理该问题)。为什么会发生这种情况?首先,您可能应该回顾一下您正在查看的内容。例如,缺少的\u类型
等同于“视频”。因此,对播放列表中的信息进行适当的检查非常简单
if info.get('_type', 'video') != 'playlist': ..
当有人在没有参数的情况下调用download
时,您的程序也会中断或执行随机操作YoutubeDL.download
需要一个url列表作为其第一个参数(名为url\u list
)
如果以前没有调用过extract\u info
,将调用YoutubeDL.download({})
。这在目前是可行的,但是{}
没有列表,因此概念上的格式是错误的。但幸运的是,这不会起什么作用
但是,如果以前调用过extract\u info
,并且您建议youtube dl不要提取详细信息(例如,您为YoutubeDL实例激活了listformats
选项),则self.info
将是一个信息列表,而不是URL列表
如果以前使用默认选项调用过extract\u info
,youtube dl将递归地调用播放列表中每个元素的extract\u info。每次调用时,您都会用条目覆盖self.info(幸运的是,据我所知,播放列表会赢,但这是难以置信的脆弱代码)。但是,每个项目都是一个字典,而不是URL。很可能,您已经选择了一个中间设置的值
子类化也可能是不必要的复杂,特别是对于Python初学者来说。它可能会使用
最后,要注意,按照目前的描述,你可能是一场灾难的受害者。基本上,youtube dl处理一个项目和一个视频的播放列表非常相似,因此区分这两个项目没有多大意义。答案太长,而且-抱歉,但是-我不明白我做错了什么。我同意lucas的观点。我不明白你的意思。你只是查看了代码,但没有回答主要问题。问题是我没有放视频,我放的是播放列表。默认情况下,我知道YoutubeDL提取所有播放列表的信息。但这种情况并没有发生。我不认为这是XY问题的一个例子。如果是,请更清楚。:)@如果是的话,你能把你的问题修改为?我觉得我所能做的就是指出您的实现的缺点,并为您的答案提供一个理论。你到底是怎么发现代码只下载了第一个索引的?此外,请务必包括您的最终目标;举例来说,为什么你讨厌单一的视频?我正在制作一个只播放列表的脚本
Playlist
是FilteredPlaylist
、LocalPlaylist
和许多不同实现的基类。我的实现使用了一个LocalPlaylist
(继承自FilteredPlaylist
——再次继承自Playlist
),但失败了。因此,我用Playlist().extract\u info(Playlist\u page\u url)
(如问题中所指出的)对其进行了测试,结果以同样的方式失败。所以这个问题更基本。问题中的代码必须有一些错误…你能发布一下你用来玩代码的代码吗?在撰写本文时,youtube dl有893个提取器,其中许多可以返回播放列表,因此行为可能会有很大差异。如果删除自定义download
方法,问题是否仍然存在?