PyTube:PrivateVideo 异常不起作用
PyTube: PrivateVideo exception not working
我写了一个代码,可以打印播放列表中所有视频的名称。问题是当它试图打印私有视频的名称时。我写了一个所有者写的 Github 的异常,但它仍然不起作用。它应该跳过这个视频并转到另一个视频,但它没有,程序崩溃了。这是我的代码:
import pytube
from pytube.exceptions import VideoPrivate
pl = pytube.Playlist("https://www.youtube.com/playlist?list=PLB1PGaMZkETOixDfsnKIOkfJS_cToCHSt")
for video in pl.videos:
try:
print(video.title)
except VideoPrivate:
continue
错误输出:
python playlist.py
One Direction - Steal My Girl
One Direction - Drag Me Down (Official Video)
One Direction - Story of My Life
One Direction - Night Changes
One Direction - Perfect (Official Video)
Traceback (most recent call last):
File "playlist.py", line 6, in <module>
for video in pl.videos:
File "C:\Users\Elton\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytube\contrib\playlist.py", line 222, in videos
yield from (YouTube(url) for url in self.video_urls)
File "C:\Users\Elton\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytube\contrib\playlist.py", line 222, in <genexpr>
yield from (YouTube(url) for url in self.video_urls)
File "C:\Users\Elton\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytube\__main__.py", line 104, in __init__
self.prefetch()
File "C:\Users\Elton\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytube\__main__.py", line 203, in prefetch
self.check_availability()
File "C:\Users\Elton\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytube\__main__.py", line 137, in check_availability
raise VideoPrivate(video_id=self.video_id)
pytube.exceptions.VideoPrivate: 8fGmghrkLco is a private video
异常是在 try/catch 之外产生的。
当 pl.videos
的下一项是 yield.
时,它实际上被提出了。
@property
def videos(self) -> Iterable[YouTube]:
"""Yields YouTube objects of videos in this playlist
:Yields: YouTube
"""
yield from (YouTube(url) for url in self.video_urls)
您可以使用 video_urls
而不是调用视频
for video in pl.video_urls:
try:
print(YouTube(video).title)
except VideoUnavailable:
continue
这里我们使用 VideoUnavailable
这是 VideoPrivate
的基类
您需要导入以下内容:
import pytube
from pytube.exceptions import VideoUnavailable
from pytube import YouTube
输出:
One Direction - Steal My Girl
One Direction - Drag Me Down (Official Video)
One Direction - Story of My Life
One Direction - Night Changes
One Direction - Perfect (Official Video)
One Direction - You & I
我写了一个代码,可以打印播放列表中所有视频的名称。问题是当它试图打印私有视频的名称时。我写了一个所有者写的 Github 的异常,但它仍然不起作用。它应该跳过这个视频并转到另一个视频,但它没有,程序崩溃了。这是我的代码:
import pytube
from pytube.exceptions import VideoPrivate
pl = pytube.Playlist("https://www.youtube.com/playlist?list=PLB1PGaMZkETOixDfsnKIOkfJS_cToCHSt")
for video in pl.videos:
try:
print(video.title)
except VideoPrivate:
continue
错误输出:
python playlist.py
One Direction - Steal My Girl
One Direction - Drag Me Down (Official Video)
One Direction - Story of My Life
One Direction - Night Changes
One Direction - Perfect (Official Video)
Traceback (most recent call last):
File "playlist.py", line 6, in <module>
for video in pl.videos:
File "C:\Users\Elton\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytube\contrib\playlist.py", line 222, in videos
yield from (YouTube(url) for url in self.video_urls)
File "C:\Users\Elton\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytube\contrib\playlist.py", line 222, in <genexpr>
yield from (YouTube(url) for url in self.video_urls)
File "C:\Users\Elton\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytube\__main__.py", line 104, in __init__
self.prefetch()
File "C:\Users\Elton\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytube\__main__.py", line 203, in prefetch
self.check_availability()
File "C:\Users\Elton\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytube\__main__.py", line 137, in check_availability
raise VideoPrivate(video_id=self.video_id)
pytube.exceptions.VideoPrivate: 8fGmghrkLco is a private video
异常是在 try/catch 之外产生的。
当 pl.videos
的下一项是 yield.
@property
def videos(self) -> Iterable[YouTube]:
"""Yields YouTube objects of videos in this playlist
:Yields: YouTube
"""
yield from (YouTube(url) for url in self.video_urls)
您可以使用 video_urls
for video in pl.video_urls:
try:
print(YouTube(video).title)
except VideoUnavailable:
continue
这里我们使用 VideoUnavailable
这是 VideoPrivate
您需要导入以下内容:
import pytube
from pytube.exceptions import VideoUnavailable
from pytube import YouTube
输出:
One Direction - Steal My Girl
One Direction - Drag Me Down (Official Video)
One Direction - Story of My Life
One Direction - Night Changes
One Direction - Perfect (Official Video)
One Direction - You & I