Have you ever spent hours filling up a sheet with data from multiple Youtube channels manually? With the YouTube API, you can do this in minutes and spend the rest of your time watching Netflix or doing anything else that you really enjoy.
This article is all about how you can use the YouTube API with Python to extract large volumes of data into an excel sheet, in minutes!
But first, what are the different use cases for marketers, when it comes to the YouTube API for Marketers?
YouTube API – Use Cases for Marketers
Extracting Influencer data:
Let’s say you have shortlisted a set of YouTube influencers and want to quickly analyze their channel performance and their content engagement.
You would either to have to pay for an expensive tool, or you would have to spend a lot of time doing manual work.
With Python and the YouTube Data API, once you have it set up, you can get this done in no time.
In this article, we will use extracting data from influencer channels on YouTube as one of the examples.
Competitor analysis:
Let’s say you want to analyze the YouTube channel content of your competitors. You can use the YouTube API to extract all their videos with the engagement data into an Excel or Google Sheet and then find their best content. You can then draw insights into what content is working best with the audience.
Finding the best performing content in a niche:
You can also find make a list of the top YouTube channels in any niche and then extract all the content from all fo them and analyze it to find the best
Find the most liked or most commented video on a YouTube channel:
When you sort videos by popularity on YouTube, it gets sorted in the descending order of “Views”.
But what if you don’t want just the most viewed but also the most liked or most commented on video from a channel. Well, this post will show you how to do that as well.
What we will do in the article
We will explore how we can use the YouTube data API with Python, by taking the below two use cases:
Use the YouTube API to extract channel data for a list of top 35 parenting influencers on YouTube:
We will take this list of 35 popular parenting influencers on YouTube and get the description, subscriber counts, total videos, and total view counts for each of the channels in an excel sheet.
Use the YouTube API to find the most liked and commented on videos on the influencers’ channels:
We will pick one of the above influencer channels and extract the data of all their videos (Title, Published date, likes, comments, dislikes, and views) into an excel sheet.
The same can be done for all the other influencers as well.
So, let’s get started!
Quick Introduction to APIs (for beginners)
As users, when we use the internet, we interact through User Interfaces. The UI is designed for users to interact with websites or apps for their everyday tasks.
An API or Application Programming Interface, on the other hand, is a software interface. It is designed to connect directly to servers for getting information at scale.
You could think of an API as a socket you could plug into, to get the information you want. You just need to have the right plug and communicate in a language that the server understands.
The way you interact with the API will vary from one API to another, which is why almost all APIs will have documentation that you can use to understand how you can communicate with it.
To give you an example, here’s the documentation for the YouTube Data API. They also have some interesting use cases and code snippets here. If this is the first time you are going through API documentation, this might seem extremely overwhelming!
Which is why we have the process simplified for you in the guide section below.
And before you get started, here’s the link to the Google Colab notebook, with the entire code used in this article, for you to follow and play along.
Guide to using the YouTube API with Python
Most of these steps like getting the YouTube API key and setting up the code will only have to be done once. You can then reuse this in the future, whenever you want to query the youtube API.
Step 1 – Getting the YouTube API key:
Log in to the Google Cloud Console and set up an account, if you don’t have one.
Next, click on “New Project. If you’re doing this for the first time, look for “Create Project”.
Next, give your project a name:
Once your project has been created, click on the menu button and click on “APIs & Services”
Next, click on “Enable APIs and Services”
Search for YouTube and click on the “YouTube Data API v3”
Enable the API
Next, click on Credentials in the left menu
Click on Create Credentials and then API key
And congratulations, you now have your API key.
A quick word of caution about API keys, especially for beginners. Store the key carefully and never ever ever post the key or code containing this key in public.
Now that we have the API key, let’s proceed with accessing the API.
Open a new Google Colab notebook:
Open up your IDE of choice. I prefer Anaconda Spyder but am going to use Google Colab for this tutorial since most people, especially those of you who are beginners would find it easy to use while getting started.
Import Dependencies:
Import the Google API Client and the Pandas module
from googleapiclient.discovery import build
import pandas as pd
Use Case 1: Use the YouTube API to extract channel data for a list of top 35 parenting influencers on YouTube
Let’s get to work on our first use case now.
Create an object:
youTubeApiKey = "AIzaSyBfXkxth00b7iLqorPskU2WfpY5NT1UtCo"
youtube = build("youtube","v3",developerKey = youTubeApiKey)
Let’s get the statistics for one channel first:
channelUsername= "WhatsUpMOMS"
channelStats = youtube.channels().list(part = "snippet,contentDetails,statistics", forUsername=channelUsername).execute()
channelStatistics = channelStats["items"][0]["statistics"]
channelStatistics
Get the Subscriber counts, video counts, and view counts:
subscriberCount = channelStatistics["subscriberCount"]
videoCount = channelStatistics["videoCount"]
viewCount = channelStatistics["viewCount"]
Get the Channel description:
channelSnippet = channelStats["items"][0]["snippet"]
channelSnippet
channelDescription = channelSnippet["description"]
channelDescription
Perfect, now that we have successfully tested and pulled all the data for one channel, let’s write the final code to pull data from all channels in one go.
Getting all the data for all the channels:
Here’s our list of channel usernames of the top 35 parenting influencers:
channelsList = [
"WhatsUpMOMS",
"TheParentingChannel",
"HighEnergyParenting",
"123MagicParenting",
"PARENTING",
"DoItOnaDime",
"handinhandparenting",
"theparentingjunkie",
"100daysofrealfood",
"AmandaMuse",
"BabyGizmoCompany",
"CandidMommy",
"CupcakeCovertops",
"CuteGirlsHairstyles",
"itsMommysLife",
"kandeejohnson",
"KittiesMama",
"MamaKatTV",
"modernmom",
"momtrends",
"MyLifeSuckers",
"PrudentBaby",
"kristenincali",
"RachhLovesLife",
"RissRoseDos",
"showmecute",
"stylebyellie",
"TheLaVigneLife",
"TheStyleDiet",
"umbumgo",
"Sprinkleofglitter",
"emilynorrisloves",
"Dollybowbow",
"LeFloofTV",
"channelmum"]
We will use the above code and loop through all the channels to pull data for each:
ChannelName=[ ]
Description=[ ]
Subscribers=[ ]
TotalVideos=[ ]
TotalViews=[ ]
for channel in channelsList:
channelUsername= channel
channelStats = youtube.channels().list(part = "snippet,contentDetails,statistics", forUsername=channelUsername).execute()
channelStatistics = channelStats["items"][0]["statistics"]
subscriberCount = channelStatistics["subscriberCount"]
videoCount = channelStatistics["videoCount"]
viewCount = channelStatistics["viewCount"]
channelSnippet = channelStats["items"][0]["snippet"]
channelDescription = channelSnippet["description"]
ChannelName.append(channelUsername)
Description.append(channelDescription)
Subscribers.append(subscriberCount)
TotalVideos.append(videoCount)
TotalViews.append(viewCount)
Exporting the data to an excel sheet:
We will now export this data to an excel sheet:
data ={"Channel Name" : ChannelName, "Description" : Description, "Subscribers" : Subscribers, "Total Videos" : TotalVideos, "Total Views" : TotalViews}
df=pd.DataFrame(data)
df.to_excel("Channels-Data.xlsx")
You will now see the Excel Sheet created in the files section of your Colab notebook and can download it from there:
And here it is! All the influencer channel data is now in the excel sheet!
Use Case 2: Use the YouTube API to find the most liked and commented on videos on the influencers’ channels
Lets now find the most liked and most commented videos from one YouTube channel:
We will take the channel WhatsUpMOMS.
Get playlist of all uploaded videos:
Let’s first get the playlist of all uploaded videos on this channel
channelUsername= "WhatsUpMOMS"
channelStats = youtube.channels().list(part = "snippet,contentDetails,statistics", forUsername=channelUsername).execute()
allUploadedVideosPlaylist = channelStats["items"][0]['contentDetails']['relatedPlaylists']['uploads']
Let’s now get the playlist data:
playlistData = youtube.playlistItems().list(playlistId=allUploadedVideosPlaylist,
part='snippet',
maxResults=50,
pageToken=next_page_token).execute()
Let get the data of all the videos in the playlist:
videos = [ ]
next_page_token = None
while True:
playlistData = youtube.playlistItems().list(playlistId=allUploadedVideosPlaylist,
part='snippet',
maxResults=50,
pageToken=next_page_token).execute()
videos += playlistData['items']
next_page_token = playlistData.get('nextPageToken')
if next_page_token is None:
break
Get engagement data for each video:
Lets now get the ids of all the videos, to be able to access their engagement statistics
video_ids=[]
for i in range(len(videos)):
video_ids.append(videos[i]["snippet"]["resourceId"]["videoId"])
i+=1
Next, let’s get the engagement statistics for each video
videoStatistics = []
for i in range(len(video_ids)):
videoData = youtube.videos().list(id=video_ids[i],part = "statistics").execute()
videoStatistics.append(videoData["items"][0]["statistics"])
i+=1
We will now loop through the entire list of videos and get their engagement data
VideoTitle=[ ]
url=[ ]
Published = [ ]
Views=[ ]
LikeCount=[ ]
DislikeCount=[ ]
Comments=[ ]
for i in range(len(videos)):
VideoTitle.append((videos[i])['snippet']['title'])
url.append("https://www.youtube.com/watch?v="+(videos[i])['snippet']['resourceId']['videoId'])
Published.append((videos[i])['snippet']['publishedAt'])
Views.append(int((videoStatistics[i])['viewCount']))
LikeCount.append(int((videoStatistics[i])['likeCount']))
DislikeCount.append(int((videoStatistics[i])['dislikeCount']))
if ("commentCount" in videoStatistics[i]):
Comments.append(int((videoStatistics[i])['commentCount']))
else:
Comments.append(0)
Exporting to Excel:
We will now export this data to an excel sheet.
data={"Video Title" : VideoTitle, "Video url" : url, "Published" : Published, "Views" : Views, "Like Count" : LikeCount, "Dislike Count" : DislikeCount, "Comments" : Comments}
df=pd.DataFrame(data)
df.to_excel("WhatsUpMOMS.xlsx")
Just like in the previous section, you will now see the Excel Sheet created in the files section of your Colab notebook and can download it from there.
We now have data from all the 827 videos that the channel has uploaded.
So, these were just a few use cases where marketers can benefit from using the YouTube API.
Here’s the link to the Colab notebook with the complete code used in this article.
What other use cases have you tried before or what other use cases can you think of, with regards to using the YouTube API in the marketing space? Do let us know in the comments below.