【PYTHON】使用Pytube從YouTube下載播放列表

2021-01-25 PYTHON

我想使用PyTube庫下載YouTube播放列表。目前,我可以一次下載一個視訊。我不能一次下載多個視訊。

目前,我的意思是

import pytube

link = input('Please enter a url link\n')
yt = pytube.YouTube(link)
stream = yt.streams.first()
finished = stream.download()
print('Download is complete')


結果為以下輸出

>> Download is complete


並且YouTube檔案已下載。當我嘗試使用播放列表連結(An example)時,只會下載第一個視訊。沒有輸出錯誤。

我希望能夠下載整個播放列表,而無需重新提示使用者。

解決辦法

您可以匯入Playlist以實現此目的。儘管GitHub repo found here中有一個部分,但重做中沒有對播放列表的引用。指令碼的原始碼在倉庫here中。

from pytube import Playlist

playlist = Playlist('https://www.youtube.com/watch?v=58PpYacL-VQ&list=UUd6MoB9NC6uYN2grvUNT-Zg')
print('Number of videos in playlist: %s' % len(playlist.video_urls))
playlist.download_all()


注意:我發現支援方法Playlist.video_urls不起作用。視訊仍被下載,但是as evidenced here

出處

Have any Question?

Let us answer it!