How to Retrieve YouTube Channel and Video Details Using Python and YouTube Data API
In this blog, we’ll explore how to use Python to fetch YouTube channel details and video information using the YouTube Data API. This guide will help you programmatically access data like channel names, descriptions, subscriber counts, video titles, views, likes, and comments using Python’s requests
library.
We will focus on fetching data for two popular YouTube channels: Cristiano Ronaldo’s official channel (@cristiano
) and MrBeast’s channel. Let’s go step-by-step on how to achieve this.
Step 1: Setting Up the Environment
Before starting, make sure you have Python installed on your machine. You’ll also need an API key from the Google Cloud Platform. You can get it by enabling the YouTube Data API v3 from the Google Developer Console.
Step 2: Install the Required Python Library
We’ll use Python’s requests
library to make HTTP requests to the YouTube Data API. Install it using pip if you haven’t already:
pip install requests
Part 1: Retrieving YouTube Channel Details
To fetch channel details, we use the channels
endpoint of the YouTube Data API. Here is a Python function to retrieve channel details such as the channel name, description, subscriber count, profile image, and more:
import requests def get_channel_details(channel_ids, api_key): channel_data = [] for channel_id in channel_ids: url = f"https://www.googleapis.com/youtube/v3/channels?part=snippet,statistics&id={channel_id}&key={api_key}" response = requests.get(url) data = response.json() if 'items' in data and len(data['items']) > 0: item = data['items'][0] snippet = item['snippet'] stats = item['statistics'] channel_data.append({ 'Channel Name': snippet['title'], 'Channel Description': snippet['description'], 'Custom URL': snippet.get('customUrl', 'N/A'), 'Profile Image': snippet['thumbnails']['high']['url'], 'Subscribers': int(stats.get('subscriberCount', 0)), 'Channel Thumbnail': snippet['thumbnails']['high']['url'] }) return channel_data # Example usage channel_ids = ["UCtxD0x6AuNNqdXO9Wp5GHew", "UCX6OQ3DkcsbYNE6H8uQQuVA"] # MrBeast and Cristiano Ronaldo channels api_key = 'YOUR_API_KEY' # Replace with your API key channel_details = get_channel_details(channel_ids, api_key) print(channel_details)
Explanation:
- API Key: Replace
'YOUR_API_KEY'
with your actual API key. - Channel IDs: The
channel_ids
list contains two channel IDs: Cristiano Ronaldo’s official channel and MrBeast’s channel. - Function
get_channel_details
:- It loops through the
channel_ids
list and constructs a URL to call the YouTube Data API’schannels
endpoint. - It retrieves the
snippet
andstatistics
parts of the channel information. - Extracted data includes
Channel Name
,Channel Description
,Custom URL
,Profile Image
,Subscribers
, andChannel Thumbnail
.
- It loops through the
- Result: The function returns a list of dictionaries containing channel details, which is then printed to the console.
Part 2: Retrieving YouTube Video Details
Once we have the channel details, we can move on to retrieving the details of videos from those channels. We’ll use the search
endpoint to get a list of recent videos and the videos
endpoint to get statistics for each video.
Python
import requests def get_video_details(channel_ids, api_key, max_results=10): video_data = [] for channel_id in channel_ids: # Get the channel name channel_url = f"https://www.googleapis.com/youtube/v3/channels?part=snippet&id={channel_id}&key={api_key}" channel_response = requests.get(channel_url) channel_data = channel_response.json() channel_name = channel_data['items'][0]['snippet']['title'] # Get the video details url = f"https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={channel_id}&order=date&maxResults={max_results}&key={api_key}" response = requests.get(url) data = response.json() for item in data.get('items', []): video_id = item['id'].get('videoId', 'N/A') if video_id != 'N/A': video_snippet = item['snippet'] video_stats = get_video_statistics(video_id, api_key) video_data.append({ 'Channel Name': channel_name, 'Video Title': video_snippet['title'], 'Video Description': video_snippet['description'], 'Video Thumbnail': video_snippet['thumbnails']['high']['url'], 'Video ID': video_id, **video_stats }) return video_data def get_video_statistics(video_id, api_key): url = f"https://www.googleapis.com/youtube/v3/videos?part=statistics&id={video_id}&key={api_key}" response = requests.get(url) data = response.json() if 'items' in data and len(data['items']) > 0: stats = data['items'][0]['statistics'] return { 'Likes': int(stats.get('likeCount', 0)), 'Comments': int(stats.get('commentCount', 0)), 'Views': int(stats.get('viewCount', 0)) } return {} # Example usage channel_ids = ["UCtxD0x6AuNNqdXO9Wp5GHew", "UCX6OQ3DkcsbYNE6H8uQQuVA"] # MrBeast and Cristiano Ronaldo channels api_key = 'YOUR_API_KEY' # Replace with your API key video_details = get_video_details(channel_ids, api_key) print(video_details)
Enjoy!