-1
import pytube

yt = YouTube('https://www.youtube.com/watch?v=lW2pXu97Yvo')

yt.captions.all()

I'm trying this simple code and keep getting this:

Error message

Can somebody recommend a method of downloading YouTube subtitles?

Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39
luka1156
  • 181
  • 1
  • 8
  • May be this link will be useful : https://stackoverflow.com/questions/44893271/python-downloading-captions-from-youtube – Mehrdad Hosseini Jan 18 '20 at 04:25
  • It is not, because even though pytube is installed the class YouTube doesn't seem to exist. :/ – luka1156 Jan 18 '20 at 05:13
  • It's quite simple, just import YouTube. You only imported pytube. If you want to use YouTube, change it YouTube to pytube.YouTube, or change `import pytube` to `from pytube import YouTube` – CrazyVideoGamer Mar 23 '21 at 17:24

2 Answers2

3

You never imported YouTube. It's supposed to be:

from pytube3 import YouTube # for Python 3
from pytube import YouTube # for Python 2
yt = YouTube('https://www.youtube.com/watch?v=lW2pXu97Yvo')

yt.captions.all()
Pyzard
  • 451
  • 3
  • 14
0

Use pytube3 instead, it has the same API but is Python3 only: https://github.com/hbmartin/pytube3

Martin
  • 770
  • 6
  • 22