admin管理员组

文章数量:1245900

from pytube import YouTube

link = input("Enter the video link here ->")

y_tube = YouTube(link)


print(y_tube.title)

Output:

Enter the video link here ->
Traceback (most recent call last):
  File "C:\Users\kumar_0nqo0ht\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytube\__main__.py", line 341, in title
    self._title = self.vid_info['videoDetails']['title']
                  ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
KeyError: 'videoDetails'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Projects\videodownloader\main.py", line 8, in <module>
    print(f'y_tube.Title->{y_tube.title}')
                           ^^^^^^^^^^^^
  File "C:\Users\kumar_0nqo0ht\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytube\__main__.py", line 346, in title
    raise exceptions.PytubeError(
pytube.exceptions.PytubeError: Exception while accessing title of . Please file a bug report at 
from pytube import YouTube

link = input("Enter the video link here ->")

y_tube = YouTube(link)


print(y_tube.title)

Output:

Enter the video link here ->https://youtu.be/rkR4sFODnIM?si=FqDP5s3TEDCMStqz
Traceback (most recent call last):
  File "C:\Users\kumar_0nqo0ht\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytube\__main__.py", line 341, in title
    self._title = self.vid_info['videoDetails']['title']
                  ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
KeyError: 'videoDetails'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Projects\videodownloader\main.py", line 8, in <module>
    print(f'y_tube.Title->{y_tube.title}')
                           ^^^^^^^^^^^^
  File "C:\Users\kumar_0nqo0ht\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytube\__main__.py", line 346, in title
    raise exceptions.PytubeError(
pytube.exceptions.PytubeError: Exception while accessing title of https://youtube/watch?v=rkR4sFODnIM. Please file a bug report at https://github/pytube/pytube
Share Improve this question edited Feb 15 at 11:52 quamrana 39.4k13 gold badges55 silver badges76 bronze badges asked Feb 15 at 11:38 Anushka kumariAnushka kumari 1 1
  • pytube has not been updated since May 2023 so it's unlikely to still function as anything dealing with YouTube is sensitive to any changes YouTube does behind the scenes (and they quite often do change things). Trying another library is your best bet, such as yt-dlp like sjalu suggested – Oliver Henriksson Commented Feb 15 at 14:09
Add a comment  | 

1 Answer 1

Reset to default 1

You can use yt_dlp library instead. Something like this:

def get_youtube_title(url):
    ydl_opts = {"quiet": True}
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        info = ydl.extract_info(url, download=False)
        return info.get('title', 'Title not found')

Hope it works!

本文标签: pythoni don39t get video title using pytubeStack Overflow