1 Sept 2023

How to extract YouTube data using YouTube api in Python?

In the contemporary digital landscape, data-driven decision-making has become a vital part of strategy planning. One area where data is bountiful is the video-sharing platform, YouTube. Whether you're a content creator wanting to increase your subscribers, or a brand wishing to evaluate your video marketing strategy, accessing and understanding YouTube statistics can offer insightful perspectives.

In this blog post, we will guide you on how to create a Python app that can fetch and analyze YouTube statistics, equipping you with the tools to glean useful insights from these numbers.

Python and YouTube's Data API

Python, known for its simplicity and readability, is a powerful language for data analysis. YouTube Data API, on the other hand, provides access to YouTube's vast database, from video views to comments, likes, and more. Marrying these two can create a robust application for fetching and analyzing YouTube data.

Firstly, to fetch data from YouTube, we need to get access to the YouTube Data API. Head over to the Google Cloud Console and create a new project. Once done, enable YouTube Data API for your project and generate an API key. Remember to keep your key secure, as it will be the bridge between your Python app and YouTube data.

Fetching Data with Python

Once you have your API key, we can start coding our Python application. Firstly, we need to install the Google API Python client. You can do this via pip:

pip install google-api-python-client

With the Google API client installed, we can now access YouTube statistics. Let's fetch data for a specific YouTube video:

from googleapiclient.discovery import build

def get_video_info(youtube, video_id):
    request = youtube.videos().list(
        part="snippet,contentDetails,statistics",
        id=video_id
    )
    response = request.execute()
    return response['items'][0]

api_key = 'your_api_key'
youtube = build('youtube', 'v3', developerKey=api_key)

video_id = 'the_ID_of_the_video'
video_info = get_video_info(youtube, video_id)

In this example, we create a function that fetches video data, including its details and statistics. We use the build function to create a YouTube API client and then execute our request.

Analyzing YouTube Statistics with Python

With data at our disposal, we can proceed to the analysis. Python's rich ecosystem offers libraries like Pandas and Matplotlib, which make data analysis and visualization a breeze.

Let's assume we want to analyze the number of views, likes, and comments a video has received over time. For this, we first need to fetch data for each day since the video's upload.

Unfortunately, YouTube API doesn't provide daily statistics. To overcome this, we'll need to make API calls for each day since the video's upload. Storing this data in a CSV file can help us track these statistics over time.

Once you have the CSV file with daily statistics, you can use Python's Pandas library to analyze the data and Matplotlib to visualize it.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('video_stats.csv')
df['date'] = pd.to_datetime(df['date'])
df.set_index('date', inplace=True)

df[['views', 'likes', 'comments']].plot(kind='line')
plt.show()

In this example, we load our CSV file into a Pandas DataFrame, set the date as our index, and plot the views, likes, and comments over time.

Conclusion

Creating a Python app to fetch and analyze YouTube statistics may seem daunting at first, but with the right tools and approach, it becomes a manageable task. Python's simplicity, coupled with YouTube's Data API, enables us to dive deep into the sea of YouTube data and extract valuable insights. Whether you're a YouTuber wanting to grow your audience or a brand aiming to understand your video engagement better, this Python app is a step in the right direction.