Python Script to Fetch Youtube Subscriber Count
To fetch the number of followers for a YouTube channel, you’ll need to use the YouTube Data API v3. Here is a code example in Python to get you started:
import requests import json # Replace <YOUR_API_KEY> with your own API key from the Google Developers Console api_key = "<YOUR_API_KEY>" # Replace <YOUTUBE_CHANNEL_ID> with the ID of the YouTube channel you want to fetch followers for channel_id = "<YOUTUBE_CHANNEL_ID>" # Make a GET request to the YouTube API to retrieve the channel details url = f"https://www.googleapis.com/youtube/v3/channels?part=statistics&id={channel_id}&key={api_key}" response = requests.get(url) # Check if the API call was successful if response.status_code == 200: # Load the API response as a JSON object data = json.loads(response.text) # Check if the API response contains the required information if "items" in data and len(data["items"]) > 0: # Extract the number of subscribers from the API response subscribers = data["items"][0]["statistics"]["subscriberCount"] # Print the number of subscribers print(f"The number of subscribers for this channel is: {subscribers}") else: print("No channel information was returned by the API") else: print("An error occurred while fetching the channel information")
Note: You will need to have the requests
and json
modules installed. You can install them by running pip install requests json
in your terminal/command prompt.