Skip to content
Related Articles

Related Articles

Get information about YouTube Channel using Python
  • Last Updated : 24 Feb, 2021

Prerequisite: YouTube API

Google provides a large set of APIs for the developer to choose from. Each and every service provided by Google has an associated API. Being one of them, YouTube Data API is very simple to use provides features like –

  • Search for videos
  • Handle videos like retrieve information about a video, insert a video, delete a video, etc.
  • Handle Subscriptions like lists all the subscriptions, insert or delete a subscription.
  • Retrieve information about comments like replies to a specific comment identified by a parentId etc.

In this article, we are going to perform various YouTube operations in Python using the YouTube API.

Get YouTube Channel Information

In this section, we will write a Python script that will extract YouTube channel information using python.

Channel Information:



  • Total Subscribers
  • Total number of videos
  • Total Views

Approach:

  • Here we will use build(), channels(), list(), execute() methods it will give YouTube channel details.
  • Inside list method, pass statistics in part property and in id property pass channelId of YouTube Channel.

Below is the Implementation:

Python3




# Import Module
from googleapiclient.discovery import build
  
# Create YouTube Object
youtube = build('youtube', 'v3'
                developerKey='Enter API key')
  
ch_request = youtube.channels().list(
    part='statistics',
    id='Enter Channel ID')
  
# Channel Information
ch_response = ch_request.execute()
  
sub = ch_response['items'][0]['statistics']['subscriberCount']
vid = ch_response['items'][0]['statistics']['videoCount']
views = ch_response['items'][0]['statistics']['viewCount']
  
print("Total Subscriber:- ", sub)
print("Total Number of Videos:- ", vid)
print("Total Views:- ", views)

Output:

Get Description of the individual video from YouTube Playlist

In this section, we will learn how to get descriptions of the individual videos from the YouTube Playlist Using Python.

Approach:

  • We will use the build, list, execute method, it will give Playlist Details.
  • Inside list method, pass snippet in part property and in playlistId property pass PlaylistID or PlaylistURL.
  • Pass 50 value in maxResults and in pageToken initially pass None Value.

Below is the Implementation:

Python3






# Import Module
from googleapiclient.discovery import build
  
  
def playlist_video_links(playlistId):
  
    nextPageToken = None
      
    # Creating youtube resource object
    youtube = build('youtube', 'v3'
                    developerKey='Enter API Key')
  
    while True:
  
        # Retrieve youtube video results
        pl_request = youtube.playlistItems().list(
            part='snippet',
            playlistId=playlistId,
            maxResults=50,
            pageToken=nextPageToken
        )
        pl_response = pl_request.execute()
  
        # Iterate through all response and get video desription
        for item in pl_response['items']:
  
            description = item['snippet']['description']
  
            print(description)
  
            print("\n")
  
        nextPageToken = pl_response.get('nextPageToken')
  
        if not nextPageToken:
            break
  
  
playlist_video_links('Enter Playlist ID')

Output:

Count Number of Playlist of YouTube Channel

In this section, we will write a Python Script which will count the number of a playlist of YouTube channels using YouTube API.

Approach:

  • Here we will use build(), playlists(), list(), execute() methods it will give YouTube channel details.
  • Inside list method, pass contentDetails and snippet in part property and in channelId property pass channelId.

Below is the Implementation:

Python3




# Import Module
from googleapiclient.discovery import build
  
# Create YouTube Object
youtube = build('youtube', 'v3'
                developerKey='Enter API key')
  
# Get video count
def Channel_Depth_details(channel_id):
    pl_request = youtube.playlists().list(
        part='contentDetails,snippet',
        channelId='channel_id',
        maxResults=50
    )
    pl_response = pl_request.execute()
  
    return len(pl_response['items'])
  
  
print(Channel_Depth_details("Channel ID"))

Output:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :