Open In App
Related Articles

How to extract youtube data in Python?

Like Article
Save Article
Report issue

Prerequisites: Beautifulsoup

YouTube statistics of a YouTube channel can be used for analysis and it can also be extracted using python code. A lot of data like viewCount, subscriberCount, and videoCount can be retrieved. This article discusses 2 ways in which this can be done.

Method 1: Using YouTube API

First we need to generate an API key. You need a Google Account to access the Google API Console, request an API key, and register your application. You can use Google APIs page to do so.

To extract data we need the channel id of the YouTube channel whose stats we want to view. To get the channel id visit that particular YouTube channel and copy the last part of the URL (In the examples given below channel id of GeeksForGeeks channel are used). 


  • First create 
  • In this file extract data using YTstats class and generate a json file will all the data extracted.
  • Now create
  • In main import
  • Add API key and channel id
  • Now using the first file data corresponding to the key given will be retrieved and saved to json file.

Example :

Code for file :


from youtube_statistics import YTstats
# paste the API key generated by you here
API_KEY = "AIzaSyA-0KfpLK04NpQN1XghxhSlzG-WkC3DHLs"
 # paste the channel id here
channel_id = "UC0RhatS1pyxInC00YKjjBqQ"
yt = YTstats(API_KEY, channel_id)


Code for file :


import requests
import json
class YTstats:
    def __init__(self, api_key, channel_id):
        self.api_key = api_key
        self.channel_id = channel_id
        self.channel_statistics = None
    def get_channel_statistics(self):
        url = f'{self.channel_id}&key={self.api_key}'
        json_url = requests.get(url)
        data = json.loads(json_url.text)
            data = data["items"][0]["statistics"]
            data = None
        self.channel_statistics = data
        return data
    def dump(self):
        if self.channel_statistics is None:
        channel_title = "GeeksForGeeks"
        channel_title = channel_title.replace(" ", "_").lower()
        # generate a json file with all the statistics data of the youtube channel
        file_name = channel_title + '.json'
        with open(file_name, 'w') as f:
            json.dump(self.channel_statistics, f, indent=4)
        print('file dumped')



Method 2: Using BeautifulSoup

Beautiful Soup is a Python library for pulling data out of HTML and XML files. In this approach we will use BeautifulSoup and Selenium to scrape data from YouTube channels. This program will tell the views, time since posted, title and urls of the videos and print them using Python’s formatting.


  • Import module
  • Provide url of the channel whose data is to be fetched
  • Extract data
  • Display data fetched.



# import required packages
from selenium import webdriver
from bs4 import BeautifulSoup
# provide the url of the channel whose data you want to fetch
urls = [
def main():
    driver = webdriver.Chrome()
    for url in urls:
        content = driver.page_source.encode('utf-8').strip()
        soup = BeautifulSoup(content, 'lxml')
        titles = soup.findAll('a', id='video-title')
        views = soup.findAll(
            'span', class_='style-scope ytd-grid-video-renderer')
        video_urls = soup.findAll('a', id='video-title')
        print('Channel: {}'.format(url))
        i = 0  # views and time
        j = 0  # urls
        for title in titles[:10]:
                                                                   views[i].text, views[i+1].text, video_urls[j].get('href')))
            i += 2
            j += 1



Last Updated : 17 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads