Get information about YouTube Channel using Python

17 May, 2022
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.
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 Modulefrom googleapiclient.discovery import build  # Create YouTube Objectyoutube = build('youtube', 'v3',                 developerKey='Enter API key')  ch_request = youtube.channels().list(    part='statistics',    id='Enter Channel ID')  # Channel Informationch_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:
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 Modulefrom 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 description        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:
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 Modulefrom googleapiclient.discovery import build  # Create YouTube Objectyoutube = build('youtube', 'v3',                 developerKey='Enter API key')  # Get video countdef 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:
simmytarika5
python-utility
Web-API
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Python | os.path.join() method
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Python | datetime.timedelta() function