YouTubeTranscriptApi #170589
Replies: 4 comments 5 replies
-
Broo! Change line:
To this
Full corrected:
Now it should run without the 'YouTubeTranscriptApi' object has no attribute 'get_transcript' error. |
Beta Was this translation helpful? Give feedback.
-
Hi @Clffordojuka, thanks for being a part of the GitHub Community! The |
Beta Was this translation helpful? Give feedback.
-
Hi @Clffordojuka, That error happens because you’re instantiating Also make sure you’re importing the right package ( Fixed & more robust versionfrom urllib.parse import urlparse, parse_qs
from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound, VideoUnavailable
def extract_video_id(url: str) -> str:
"""
Handles typical YouTube URLs:
- https://www.youtube.com/watch?v=VIDEO_ID
- https://youtu.be/VIDEO_ID
- with extra query params
"""
parsed = urlparse(url)
if parsed.hostname in {"www.youtube.com", "youtube.com"}:
qs = parse_qs(parsed.query)
if "v" in qs:
return qs["v"][0]
# fallback for youtu.be or raw IDs
return parsed.path.lstrip("/")
def fetch_youtube_transcript(video_url: str) -> str:
"""
Fetch YouTube transcript text for the given URL.
Tries German first, then English.
"""
video_id = extract_video_id(video_url)
try:
# ✅ call on the class, NOT an instance
transcript_data = YouTubeTranscriptApi.get_transcript(
video_id, languages=["de", "en"]
)
return " ".join(entry["text"] for entry in transcript_data)
except TranscriptsDisabled:
raise RuntimeError("Subtitles are disabled for this video.")
except NoTranscriptFound:
raise RuntimeError("No transcript found for 'de' or 'en'.")
except VideoUnavailable:
raise RuntimeError("Video is unavailable or the ID is wrong.")
if __name__ == "__main__":
print(fetch_youtube_transcript("https://www.youtube.com/watch?v=dQw4w9WgXcQ")) If it still fails on one machine/server
Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
The error happens because you’re instantiating YouTubeTranscriptApi as an object, but it’s actually a class with static methods — you should call its methods directly, not via an instance.
Change
to
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Select Topic Area
Question
Body
I am getting this error when I am trying to test both on the machine and the server"An error occurred: 'YouTubeTranscriptApi' object has no attribute 'get_transcript'"
from youtube_transcript_api import YouTubeTranscriptApi
def fetch_youtube_transcript(video_url: str) -> str:
"""
Fetch YouTube transcript text for the given URL.
"""
if "v=" in video_url:
video_id = video_url.split("v=")[-1].split("&")[0]
else:
video_id = video_url.split("/")[-1]
transcript_data = YouTubeTranscriptApi().get_transcript(video_id, languages=['de', 'en'])
transcript_text = " ".join([entry['text'] for entry in transcript_data])
return transcript_text
Beta Was this translation helpful? Give feedback.
All reactions