YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1

Patrick Loeber · Beginner ·🛠️ AI Tools & Apps ·6y ago

Key Takeaways

This video tutorial demonstrates how to use the YouTube Data API v3 with Python to analyze channel statistics, including setting up an app, extracting basic channel statistics, and dumping data into a JSON file. Tools such as Google Developer Console, YouTube Data API v3, and pandas are utilized.

Full Transcript

hey guys welcome to a new Python tutorial this is going to be a small four-part series in which we will learn how to work with the YouTube data API we will develop a project that can automatically download channel and video statistics for a given channel and then we are going to create a - pi - notebook in which we can plot and analyze the data so before we begin let me show you how the final project looks like so the final code will download and dump all the channel data into a JSON file that looks like this so here we have some channel statistics for example we can see the view count the subscriber count and the video count and then for each of the videos we have more information for example here we see the date where it got published and we see the title we see the description and we even have the links to the thumbnails and then if we scroll down further we see some statistics like the view count of this video the light count the dislike count and the comment count and the duration so yeah and there are a lot of more information so there's so much that we can do with this data and then after we've downloaded this Chasen file we will go ahead and create a chip ID a notebook and analyze this data a little bit so let me show you the notebook so the notebook will look like this and for this we are going to use pandas and the first thing we do is to load the chase and file and then for example we print the channel statistics like the views to subscribers and the videos and then we are going to analyze the most popular videos for example here we can plot the top 10 most viewed videos in my channel for example you can see that the K nearest neighbors tutorial is currently the most popular one with the most views and we also see the likes and the dislikes and how many comments and got so a lot of information for this video here and we also plot the most viewed videos so here we plot the view count and we also can analyze the least viewed videos and compare so here we have the least viewed videos and then for example we compare the like versus dislike ratio of the most popular and the least popular videos and we also plot the likes and the dislikes and yeah so there's so much that we can do with this data but I will leave this up to you then so yeah that's the project that we are going to develop and I will split this tutorial into four parts so in this first part we are learning how we register our app and use the API and then download channel statistics in the second part we see how we can get a list of all the videos with some basic information in the third part we will learn how we can get in-depth statistics for each video and then in the last part we can see how we create the two pod a notebook that I just showed you so yeah let's get started and the first thing that we need to do in order to use the YouTube data API is to register our app in the Google developer console and get an API key so for this we want to head over to this website so Google so consult out developers.google.com and for this you're going to need a gmail account and then sign in with your email and now when you're signed in the next thing you want to do is to create a new project so I already have one here but let's create a new one so let's click on this drop down menu and select new project and now we have to type in a name for this project so let's say YouTube one because I already have a one with the name YouTube so and let's click on create and this will take a couple of seconds and now once this got created we can select this so now I've got created so let's click on this drop down menu here again and select this new project and now the next thing we want to do is to enable the YouTube data so let's click on enable api's and services and here we see all the api's that Google provides for us for example we also can work with the Google Calendar API or the Gmail API and in our case we want to use the YouTube data API version 3 so let's click on this and then we want to click on enable so let's click on enable and this will also take a couple of seconds and now it got enabled so now the last thing we need to do is to get an API key so let's click on credentials here and let's click on create credentials and then select API key and this will create an API key for this project so let's copy this to the clipboard and in a couple of seconds we will paste this into our project so now we have everything that we need so now we can close this and now let's create a new folder for our project so in my terminal I can say make Durer and let's call this YouTube and let's go to this folder and open up our editor and now I want to create two files so the first one is the main PI file and the second one I will call let's say YouTube underscore statistics and in our main file I will paste the API key so I will create a variable API key equals and this is a string and now I will hit paste and this will paste the API key here so and now we can implement the YouTube statistics algorithm so for this we are going to create a class and let's call this YT stats for YouTube statistics and this will have an init so let's define the pin it this will have of course and then we will pass the API key and also the channel ID for this so let's say API key and channel ID and then we will simply store this here so we will say self dot API key equals API key and self dot channel ID equals channel ID and we will also create an object for the channel statistics so let's say self dot Channel statistics equals so in the beginning this is just none and now let's create a function in which we will get this channel statistics so define get channel statistics and this will only have self and now we need the URL for this so let's create a URL object your l equals and for this we are using a f string and let's head over to the official documentation so that you can find this here on developers.google.com slash YouTube / version / v3 / Docs and yeah I will recommend - that you check this out for yourself so in our case we want to search for channels so channels and here we have it so this is the URL and then we also see that we need to put in a slash channel so let's copy and paste this here and let's say self channels and now let me tell you the final URL so we are interested in the statistics part so let's say question mark part equals statistics and we also have to give it the channel IDs so we say end ID equals and now let's use braces in our f string and then we can simply say self dot channel ID and the last thing that we have to append to this URL is our API key so we say end and then key equals and now again braces and then self dot API key so this is the final URL and let's print this so let's say print URL and save this and now let's head over to the main file and now let's import this first so let's say from YouTube statistics import YT stats now we have this and now we can create an object here so let's say YT equals YouTube stats and this will need to have the API key so the API key and this also needs the channel ID so let's create the channel ID here channel ID equals and you can analyze any channel that you like so let's head over to youtube.com and let's analyze my own channel so let's search for Python engineer and here we have the channel and if we click on this then here in the URL so the last part is the channel ID so let's copy this and let's paste this here into our string and now we have the channel ID so let's pass this to our YouTube statistics object and then let's call YT dot get channel statistics so the function that we just created and let's open up a terminal and I will also activate my virtual environment so I can I use condo for this so I say kondeh activate pi ang on the score and so this is the name of this environment and let's simply run this so let's say python hi and yeah so this works so here we have the URL so I will open up this URL so this opens up the URL in the browser and here we see that we have a list with some information and this is actually valid JSON format and here for example we have some channel statistics that we are interested in for example we have the view count the subscribe account and the video count so let's get this in Python and for this we need to send a get request to this website so for this we are going to use the awesome requests module so we import re quests and if you haven't installed this already then you can simply do this by saying pip install requests and put this in your terminal and now let's send a get request with this module so we can simply say Jason URL equals requests dot get this URL and as you've just seen this is already valid JSON format so we can convert this to a Python object by using the Jason module so import Jason and now we say data equals and then we say jason dot load s so from a string and here we have to chase URL and we need to call dot txt to get the actual text of the website and now let's print the data so let's comment this out here and save this and now let's run the main file again so python main dot pi and now this should print out the same information that we've just seen on the web side so yeah so here we can we have the stat statistics part so we can see the view count the subscribe account and the video count so we are only interested in these three information so let's filter this Chasen object so if we have a look at the website again so then we see that the important part is in the items key and this is a list with only one object so let's get only the items key and then the first item in this list and we will wrap this in a try except block so if there are any arrows then this will not crash so let's say try and now let's filter this so here we say data equals data and now this is a dictionary where we can access the items parts of items and then as I just showed you this is a list so we will only only want to have the first item in this list so we access this by saying and the 0 0 with element and now let's have a look at the statistics again so here in this list so now this is the list that we have and then there's another key statistics and this is all the information that we want so let's access this statistics part by saying brackets and then statistics statistics and in let's say except if there are any arrows for example if our API key is not valid then this is not a valid JSON data with these keys so in this case we simply say data equals none and then let's return the data so they return data and let's save this and here we can say data equals this and then it's printed data here print data so let's comment this out here again so no more printing in our actual function so let's save this and again let's let me clear this here and let's run the main file again and yeah so now we see that we only have this part that we are interested in so here we have the statistics of my channel so we can see the view count and the subscriber count and the video count and yeah so now let's write one more function that will dump this to the chase and file so one more thing that I forgot here is now we want to store this data into the channel statistics object so we simply say self dot channel statistics equals data and now let's write another function and call this defined dump and this only has self as an object here as a parameter here and we will first we will check if self dot channel statistics is none and then we return because we have to call this method first because before we can call the dump method and now if this is not none and we can continue and dump this to a JSON file so let's get the the name of the file first and this should be the name of the channel so let's say channel title equals and in this case it's Python engineer but we actually want to get this from the now chase an object so I will leave this as a to do for the next video so here I can say get channel name from data and then I want to convert this to a lowercase and I also want to replace all the spaces with underscores so I can do this by saying channel title equals channel title dot replace so this is a built-in method for Strings and I want to replace spaces with underscores and I also want to convert this to lower cases only by saying dot lower and now I have the name of the file so I can say the file name equals channel title plus dot jason because this should be a JSON file and now we want to open this so we can say with open file name and we want to open this in write modes and then let's open this as F and here we can say jason dot dum and then we want to dump our self dot channel statistics into this file and we also want to say in dent equals four so this is an optional parameter that will make the final file look nicer and yeah so now we are done so let's simply print file dumped and let's head over to the main file again and let me also open up the Explorer and now let's say well let's first we want to call the get statistics method and once this is finished we say why T dot dumb and let's save this then let's clear this and run this one more time so Pythian maned up hi and now it's printed file dumped so this worked and now we have our JSON file here so python engineered or chasing so let's have a look so yeah here we have all the channel statistics and yeah this is what I wanted to do in the first part so in the next part we are continuing by getting the list of all the videos and yeah I hope you enjoyed this tutorial if you like this please subscribe to the channel and see you next time bye

Original Description

In this Python Tutorial we will be learning how to work with the YouTube Data API and analyze channel statistics. This is going to be a 4 part series: - Part 1: Setup your app and get API Key, and extract basic channel statistics - Part 2: Get a list of all the videos of a channel - Part 3: Get in depth statistics for each video - Part 4: Analyze the data in a jupyter notebook ~~~~~~~~~~~~~~ GREAT PLUGINS FOR YOUR CODE EDITOR ~~~~~~~~~~~~~~ ✅ Write cleaner code with Sourcery: https://sourcery.ai/?utm_source=youtube&utm_campaign=pythonengineer * 📚 Get my FREE NumPy Handbook: https://www.python-engineer.com/numpybook 📓 Notebooks available on Patreon: https://www.patreon.com/patrickloeber ⭐ Join Our Discord : https://discord.gg/FHMg9tKFSN If you enjoyed this video, please subscribe to the channel! The code can be found here: https://github.com/patrickloeber/youtube-analyzer More: - Official guides and docs: https://developers.google.com/youtube/v3 - Register app: https://console.developers.google.com/ You can find me here: Website: https://www.python-engineer.com Twitter: https://twitter.com/patloeber GitHub: https://github.com/patrickloeber #Python #Coding #Tutorial ---------------------------------------------------------------------------------------------------------- * This is a sponsored link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Patrick Loeber · Patrick Loeber · 55 of 60

1 Lists in Python - Advanced Python 01 - Programming Tutorial
Lists in Python - Advanced Python 01 - Programming Tutorial
Patrick Loeber
2 Tuples in Python - Advanced Python 02 - Programming Tutorial
Tuples in Python - Advanced Python 02 - Programming Tutorial
Patrick Loeber
3 Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Patrick Loeber
4 Sets in Python - Advanced Python 04 - Programming Tutorial
Sets in Python - Advanced Python 04 - Programming Tutorial
Patrick Loeber
5 Strings in Python - Advanced Python 05 - Programming Tutorial
Strings in Python - Advanced Python 05 - Programming Tutorial
Patrick Loeber
6 Collections in Python - Advanced Python 06 - Programming Tutorial
Collections in Python - Advanced Python 06 - Programming Tutorial
Patrick Loeber
7 Itertools in Python - Advanced Python 07 - Programming Tutorial
Itertools in Python - Advanced Python 07 - Programming Tutorial
Patrick Loeber
8 Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Patrick Loeber
9 Exceptions in Python - Advanced Python 09 - Programming Tutorial
Exceptions in Python - Advanced Python 09 - Programming Tutorial
Patrick Loeber
10 Logging in Python - Advanced Python 10 - Programming Tutorial
Logging in Python - Advanced Python 10 - Programming Tutorial
Patrick Loeber
11 JSON in Python - Advanced Python 11 - Programming Tutorial
JSON in Python - Advanced Python 11 - Programming Tutorial
Patrick Loeber
12 Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Patrick Loeber
13 Decorators in Python - Advanced Python 13 - Programming Tutorial
Decorators in Python - Advanced Python 13 - Programming Tutorial
Patrick Loeber
14 Generators in Python - Advanced Python 14 - Programming Tutorial
Generators in Python - Advanced Python 14 - Programming Tutorial
Patrick Loeber
15 Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Patrick Loeber
16 Threading in Python - Advanced Python 16 - Programming Tutorial
Threading in Python - Advanced Python 16 - Programming Tutorial
Patrick Loeber
17 Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Patrick Loeber
18 Function arguments in detail - Advanced Python 18 - Programming Tutorial
Function arguments in detail - Advanced Python 18 - Programming Tutorial
Patrick Loeber
19 The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
Patrick Loeber
20 Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Patrick Loeber
21 Context Managers in Python - Advanced Python 21 - Programming Tutorial
Context Managers in Python - Advanced Python 21 - Programming Tutorial
Patrick Loeber
22 KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
Patrick Loeber
23 Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Patrick Loeber
24 Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Patrick Loeber
25 Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Patrick Loeber
26 Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Patrick Loeber
27 Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Patrick Loeber
28 SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
Patrick Loeber
29 Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Patrick Loeber
30 Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Patrick Loeber
31 Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Patrick Loeber
32 PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
Patrick Loeber
33 K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
Patrick Loeber
34 Anaconda Tutorial - Installation and Basic Commands
Anaconda Tutorial - Installation and Basic Commands
Patrick Loeber
35 PyTorch Tutorial 01 - Installation
PyTorch Tutorial 01 - Installation
Patrick Loeber
36 PyTorch Tutorial 02 - Tensor Basics
PyTorch Tutorial 02 - Tensor Basics
Patrick Loeber
37 PyTorch Tutorial 03 - Gradient Calculation With Autograd
PyTorch Tutorial 03 - Gradient Calculation With Autograd
Patrick Loeber
38 PyTorch Tutorial 04 - Backpropagation - Theory With Example
PyTorch Tutorial 04 - Backpropagation - Theory With Example
Patrick Loeber
39 PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
Patrick Loeber
40 PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
Patrick Loeber
41 PyTorch Tutorial 07 - Linear Regression
PyTorch Tutorial 07 - Linear Regression
Patrick Loeber
42 PyTorch Tutorial 08 - Logistic Regression
PyTorch Tutorial 08 - Logistic Regression
Patrick Loeber
43 PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
Patrick Loeber
44 PyTorch Tutorial 10 - Dataset Transforms
PyTorch Tutorial 10 - Dataset Transforms
Patrick Loeber
45 Download Images With Python Automatically - Python Web Scraping Tutorial
Download Images With Python Automatically - Python Web Scraping Tutorial
Patrick Loeber
46 PyTorch Tutorial 11 - Softmax and Cross Entropy
PyTorch Tutorial 11 - Softmax and Cross Entropy
Patrick Loeber
47 Select Movies with Python - Web Scraping Tutorial
Select Movies with Python - Web Scraping Tutorial
Patrick Loeber
48 PyTorch Tutorial 12 - Activation Functions
PyTorch Tutorial 12 - Activation Functions
Patrick Loeber
49 List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
Patrick Loeber
50 PyTorch Tutorial 13 - Feed-Forward Neural Network
PyTorch Tutorial 13 - Feed-Forward Neural Network
Patrick Loeber
51 How To Add A Progress Bar In Python With Just One Line - Python Tutorial
How To Add A Progress Bar In Python With Just One Line - Python Tutorial
Patrick Loeber
52 PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
Patrick Loeber
53 The Walrus Operator - New in Python 3.8 - Python Tutorial
The Walrus Operator - New in Python 3.8 - Python Tutorial
Patrick Loeber
54 PyTorch Tutorial 15 - Transfer Learning
PyTorch Tutorial 15 - Transfer Learning
Patrick Loeber
YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
Patrick Loeber
56 YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
Patrick Loeber
57 YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
Patrick Loeber
58 YouTube Data API Tutorial with Python - Analyze the Data - Part 4
YouTube Data API Tutorial with Python - Analyze the Data - Part 4
Patrick Loeber
59 AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
Patrick Loeber
60 Ultimate FREE Study Guide for Machine Learning and Deep Learning
Ultimate FREE Study Guide for Machine Learning and Deep Learning
Patrick Loeber

This tutorial teaches how to use the YouTube Data API v3 with Python to analyze channel statistics. It covers setting up an app, extracting basic channel statistics, and dumping data into a JSON file. By following this tutorial, viewers can learn how to work with the YouTube Data API and perform data analysis using Python.

Key Takeaways
  1. Register app in Google Developer Console
  2. Enable YouTube Data API v3
  3. Download channel statistics using YouTube Data API v3
  4. Dump channel data into a JSON file
  5. Load JSON file using pandas
  6. Create class YTStats to handle YouTube statistics
  7. Implement get_channel_statistics method to retrieve channel statistics from YouTube Data API
💡 The YouTube Data API v3 can be used with Python to analyze channel statistics, and the data can be dumped into a JSON file for further analysis.

Related AI Lessons

I Built a Free AI-Powered YouTube SEO Toolkit With Zero Budget. Here’s What Actually Happened.
Learn how a solo dev built a free AI-powered YouTube SEO toolkit with zero budget and the lessons they learned from the experience
Medium · Startup
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Learn to create a second version of yourself inside Obsidian using AI with a step-by-step guide
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Learn how to prepare for the Spain civil service TIC exam using AI in 2026, boosting your chances of success with technology-driven study techniques
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Create viral AI kissing videos using AIAI.com in a step-by-step process, leveraging AI technology for creative content creation
Medium · AI
Up next
Low-Tech, High-Impact: Replacing Your Receptionist With a $15 AI Phone System
Maximum Lawyer
Watch →