File size: 1,270 Bytes
685331f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
.. _exceptions:

Exception handling
==================

Pytube implements a number of useful exceptions for handling program flow.
There are a number of cases where pytube simply cannot access videos on YouTube
and relies on the user to handle these exceptions. Generally speaking, if a
video is unaccessible for any reason, this can be caught with the generic
VideoUnavailable exception. This could be used, for example, to skip private
videos in a playlist, videos that are region-restricted, and more.

Let's see what your code might look like if you need to do exception handling::

    >>> from pytube import Playlist, YouTube
    >>> playlist_url = 'https://youtube.com/playlist?list=special_playlist_id'
    >>> p = Playlist(playlist_url)
    >>> for url in p.video_urls:
    ...     try:
    ...         yt = YouTube(url)
    ...     except VideoUnavailable:
    ...         print(f'Video {url} is unavaialable, skipping.')
    ...     else:
    ...         print(f'Downloading video: {url}')
    ...         yt.streams.first().download()

This will automatically skip over videos that could not be downloaded due to a
limitation with the pytube library. You can find more details about what
specific exceptions can be handled here: :py:mod:`pytube.exceptions`.