"AttributeError: no attribute 'download " With PyTube

"AttributeError: no attribute 'download " With PyTube

我在使用 YouTube 时遇到 pytube 问题

from pytube import YouTube
url = str(input("Youtube video url :"))
youtube = YouTube(url)
stream = youtube.streams()
video.download(0)

此错误显示

AttributeError: 'YouTube' object has no attribute 'download'

当我使用播放列表时

from pytube import Playlist
playlist = Playlist('https://www.youtube.com/playlist?list=PLRfY4Rc-GWzhdCvSPR7aTV0PJjjiSAGMs')
print('Number of videos in playlist: %s' % len(playlist.video_urls))
playlist.download_all()

显示相同的错误

AttributeError: 'Playlist' object has no attribute 'download_all'

您需要从您创建的 YouTube 对象的 Stream 中获取第一个流。如果你看 the documentation 你可能已经知道了。

试试这个来下载视频:

from pytube import YouTube
url = str(input("Youtube video url :"))
youtube = YouTube(url)
youtube.streams.first().download()

对于播放列表试试这个:

from pytube import Playlist
playlist = Playlist('https://www.youtube.com/playlist?list=PLRfY4Rc-GWzhdCvSPR7aTV0PJjjiSAGMs')
print('Number of videos in playlist: %s' % len(playlist.video_urls))

# Loop through all videos in the playlist and download them
for video in playlist.videos:
    video.streams.first().download()

Youtube 功能没有“下载”功能,即使用 Youtube 我们无法下载视频。

例如: yt = YouTube(url).download() #output = 错误

我们只能使用流来下载视频。不需要导入流。

例如: yt = YouTube(url).streams.download() #output = 成功