如何使用 pytube 下载 youtube 播放列表视频?
How to download youtube playlist video using pytube?
我正在使用 Python 3.8.5.
#pip freeze
beautifulsoup4==4.9.1
bs4==0.0.1
packaging==20.4
pyparsing==2.4.7
PyQt5==5.15.0
PyQt5-sip==12.8.0
PyQtWebEngine==5.15.0
pytube3==9.6.4
sip==5.3.0
six==1.15.0
soupsieve==2.0.1
toml==0.10.1
typing-extensions==3.7.4.2
代码
from pytube import Playlist
playlist = Playlist('https://www.youtube.com/playlist?list=PL6gx4Cwl9DGCkg2uj3PxUWhMDuTw3VKjM')
print('Number of videos in playlist: %s' % len(playlist.video_urls))
for video_url in playlist.video_urls:
print(video_url)
playlist.download_all()
警告
Number of videos in playlist: 0 playlist.py:24: DeprecationWarning:
Call to deprecated function download_all (This function will be
removed in the future. Please iterate through .videos).
playlist.download_all() /media/hophoet/Nouveau
nom/Projects/python/workspace/automates/autovideo/venv/lib/python3.8/site-packages/pytube/contrib/playlist.py:216:
DeprecationWarning: Call to deprecated function
_path_num_prefix_generator (This function will be removed in the future.).
根据一些研究,pytube3==9.6.4 和 YouTube 的 HTML 代码之间似乎存在问题。该问题与 pytube3 如何读取 YouTube URL 有关。下面的代码通过使用与 YouTube 更新后的 HTML 代码匹配的正则表达式解决了这个问题。
import re
from pytube import Playlist
playlist = Playlist("https://www.youtube.com/playlist?list=PL6gx4Cwl9DGCkg2uj3PxUWhMDuTw3VKjM")
playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")
print('Number of videos in playlist: %s' % len(playlist.video_urls))
for url in playlist.video_urls:
print(url)
###############################
OUTPUT
###############################
Number of videos in playlist: 23
https://www.youtube.com/watch?v=HjuHHI60s44
https://www.youtube.com/watch?v=Z40N7b9NHTE
https://www.youtube.com/watch?v=FvziRqkLrEU
https://www.youtube.com/watch?v=XN2-87haa8k
https://www.youtube.com/watch?v=VgI4UKyL0Lc
https://www.youtube.com/watch?v=BvPIgm2SMG8
https://www.youtube.com/watch?v=DpdmUmglPBA
https://www.youtube.com/watch?v=BmVmJi5dR9c
https://www.youtube.com/watch?v=pYNuKXjcriM
https://www.youtube.com/watch?v=EWONqLqSxYc
https://www.youtube.com/watch?v=EKmLXiA4zaQ
https://www.youtube.com/watch?v=-DHCm9AlXvo
https://www.youtube.com/watch?v=7cRaGaIZQlo
https://www.youtube.com/watch?v=ZkcEB96iMFk
https://www.youtube.com/watch?v=5Fcf-8LPvws
https://www.youtube.com/watch?v=xWLgdSgsBFo
https://www.youtube.com/watch?v=QcKYFEgfV-I
https://www.youtube.com/watch?v=BtSQIxDPnLc
https://www.youtube.com/watch?v=O5kh_-6e4kk
https://www.youtube.com/watch?v=RuWVDz-48-o
https://www.youtube.com/watch?v=-yjc5Y7Wbmw
https://www.youtube.com/watch?v=C5T59WsrNCU
https://www.youtube.com/watch?v=MWldNGdX9zE
我确实注意到 pytube3 目前不受支持,有人将它分叉到 pytubeX。我试图弄清楚下载,因为我无法让那件作品与 pytube3 或 pytubex 一起使用。我会继续关注这个问题。
我正在使用 Python 3.8.5.
#pip freeze
beautifulsoup4==4.9.1
bs4==0.0.1
packaging==20.4
pyparsing==2.4.7
PyQt5==5.15.0
PyQt5-sip==12.8.0
PyQtWebEngine==5.15.0
pytube3==9.6.4
sip==5.3.0
six==1.15.0
soupsieve==2.0.1
toml==0.10.1
typing-extensions==3.7.4.2
代码
from pytube import Playlist
playlist = Playlist('https://www.youtube.com/playlist?list=PL6gx4Cwl9DGCkg2uj3PxUWhMDuTw3VKjM')
print('Number of videos in playlist: %s' % len(playlist.video_urls))
for video_url in playlist.video_urls:
print(video_url)
playlist.download_all()
警告
Number of videos in playlist: 0 playlist.py:24: DeprecationWarning: Call to deprecated function download_all (This function will be removed in the future. Please iterate through .videos).
playlist.download_all() /media/hophoet/Nouveau nom/Projects/python/workspace/automates/autovideo/venv/lib/python3.8/site-packages/pytube/contrib/playlist.py:216: DeprecationWarning: Call to deprecated function _path_num_prefix_generator (This function will be removed in the future.).
根据一些研究,pytube3==9.6.4 和 YouTube 的 HTML 代码之间似乎存在问题。该问题与 pytube3 如何读取 YouTube URL 有关。下面的代码通过使用与 YouTube 更新后的 HTML 代码匹配的正则表达式解决了这个问题。
import re
from pytube import Playlist
playlist = Playlist("https://www.youtube.com/playlist?list=PL6gx4Cwl9DGCkg2uj3PxUWhMDuTw3VKjM")
playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")
print('Number of videos in playlist: %s' % len(playlist.video_urls))
for url in playlist.video_urls:
print(url)
###############################
OUTPUT
###############################
Number of videos in playlist: 23
https://www.youtube.com/watch?v=HjuHHI60s44
https://www.youtube.com/watch?v=Z40N7b9NHTE
https://www.youtube.com/watch?v=FvziRqkLrEU
https://www.youtube.com/watch?v=XN2-87haa8k
https://www.youtube.com/watch?v=VgI4UKyL0Lc
https://www.youtube.com/watch?v=BvPIgm2SMG8
https://www.youtube.com/watch?v=DpdmUmglPBA
https://www.youtube.com/watch?v=BmVmJi5dR9c
https://www.youtube.com/watch?v=pYNuKXjcriM
https://www.youtube.com/watch?v=EWONqLqSxYc
https://www.youtube.com/watch?v=EKmLXiA4zaQ
https://www.youtube.com/watch?v=-DHCm9AlXvo
https://www.youtube.com/watch?v=7cRaGaIZQlo
https://www.youtube.com/watch?v=ZkcEB96iMFk
https://www.youtube.com/watch?v=5Fcf-8LPvws
https://www.youtube.com/watch?v=xWLgdSgsBFo
https://www.youtube.com/watch?v=QcKYFEgfV-I
https://www.youtube.com/watch?v=BtSQIxDPnLc
https://www.youtube.com/watch?v=O5kh_-6e4kk
https://www.youtube.com/watch?v=RuWVDz-48-o
https://www.youtube.com/watch?v=-yjc5Y7Wbmw
https://www.youtube.com/watch?v=C5T59WsrNCU
https://www.youtube.com/watch?v=MWldNGdX9zE
我确实注意到 pytube3 目前不受支持,有人将它分叉到 pytubeX。我试图弄清楚下载,因为我无法让那件作品与 pytube3 或 pytubex 一起使用。我会继续关注这个问题。