Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

Youtube Channel List not generated using Youtube API

$
0
0

I am trying to create a script to generate a list of Youtube channels depending on their number of subscribers and country.The count can be obviously more than 50.

import requestsimport jsonimport csvimport os# Get the YouTube API key from the environment variableAPI_KEY = 'My_API_KEY'# Set the search parametersMIN_SUBSCRIBERS = 1500000MAX_SUBSCRIBERS = 2300000COUNTRY_CODES = ['US', 'GB']# Create a list to store the YouTube channelschannels = []# Create a request URL for each country codefor country_code in COUNTRY_CODES:    request_url = 'https://www.googleapis.com/youtube/v3/channels?part=snippet,statistics&maxResults=50&regionCode={}&minSubscribers={}&maxSubscribers={}'.format(        country_code, MIN_SUBSCRIBERS, MAX_SUBSCRIBERS    )    # Make the request    response = requests.get(request_url, headers={'Authorization': 'Bearer {}'.format(API_KEY)})    # Check the response status code    if response.status_code == 200:        # Get the JSON response data        data = json.loads(response.content)        # Iterate over the channels in the response        for channel in data['items']:            # Add the channel to the list            channels.append(channel)# Create an Excel spreadsheet to store the resultswith open('youtubers.csv', 'w', newline='') as csvfile:    writer = csv.writer(csvfile, delimiter=',')    writer.writerow(['Channel Name', 'Channel Link', 'Number of Subscribers', 'Contact Email'])    # Iterate over the channels in the list    for channel in channels:        # Get the channel name        channel_name = channel['snippet']['title']        # Get the channel link        channel_link = channel['snippet']['url']        # Get the number of subscribers        number_of_subscribers = channel['statistics']['subscriberCount']        # Get the contact email (if available)        contact_email = channel.get('snippet', {}).get('contactEmail')        # Write the channel details to the Excel spreadsheet        writer.writerow([channel_name, channel_link, number_of_subscribers, contact_email])print('The list of YouTubers has been generated.')

I want to know what I am doing wrong.It's generating the .csv file but does not generate any list.


Viewing all articles
Browse latest Browse all 3831

Trending Articles