How to Extract Captions from YouTube Using Python?

At times, we feel that it’s good to read documentation rather than watch a video on YouTube to get some information. Therefore, this blog could be helpful for you in scraping subtitles from YouTube in the text file and generating subtitles from video Python.

Installation

Use the command given below and you will be able to install a Python API, helping you to extract subtitles data from YouTube videos.

pip install youtube_transcript_api

Procedure

Initially, import YouTubeTranscript on or after youtube_transcript.

from youtube_transcript_api import YouTubeTranscriptApi

Save the subtitles acquired by .get_transcript() function within the variable called srt like a listing of dictionaries.

srt = YouTubeTranscriptApi.get_transcript("kvTV2ZmVpmg&t=32s")

Create or overwrite a file called “subtitles.txt” having details inside a context manager as well as it repeats through every element of the list srt. Lastly, it writes every element of the srt on given new line within “subtitles.txt” text file.

with open("subtitles.txt", "w") as f:
for i in srt:
f.write("{}\n".format(i))

Hurrah…We have successfully scraped subtitles from YouTube in the text file using our YouTube data Extractor using Python. If you want to know more about how to get all the captions from a YouTube video then contact 3i Data Scraping or ask for a free quote!

Originally published at https://www.3idatascraping.com.

--

--

--

3i Data Scraping is an Experienced Web Scraping Service Provider in the USA. We offering a Complete Range of Data Extraction from Websites and Online Outsource.

Recommended from Medium

Engine Assembly Manufacturing Plant

When your Agile Requirements go Wrong, Part 2

When your Agile Requirements go Wrong, Part 2

DefiCliq secures an incredible Partnership with API3

How to Make Cockroach DB Faster

HTML generation in Clojure with Enlive

How To Be a Developer That People Want on Their Team

Hosting an app on heroku simplified

Recap of the Levana Meteor Shower

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
3i Data Scraping

3i Data Scraping

3i Data Scraping is an Experienced Web Scraping Service Provider in the USA. We offering a Complete Range of Data Extraction from Websites and Online Outsource.

More from Medium

The complete guide to proxies for web scraping

Web scrapping Instagram with Python & Selenium

How Does Python Help In Web Scraping?

A Django Todo app — learn from scratch