'I need a way to pull 100 of the most popular channels and their ID's from YouTube using the youtube api in the python language

http://www.youtube.com/dev/ ive already checked the youtube api dev info and have not found anything pertaining to this.



Solution 1:[1]

Obtain API youtube To be able to make requests of this type you must have an API key,

  1. Go to link https://console.developers.google.com/
  2. Create a new project
  3. find youtube Data API v3 and click on it

3) find youtube Data API v3 and click on it

  1. Enable the API
  2. go to credentials and create one for the project go to credentials and create one for the project
  1. write it down and insert it in the script below

This script uses ÁPI created previously to make requests on the channels by creating name in a random way and inserts the data in two files in the first it stores all the info in the second only the id, the channel name and the link to the channel, I hope it is what you are looking for ;)

import json
import urllib.request
import string
import random

channels_to_extract = 100
API_KEY = '' #your api key 

while True:
    
    random_name = ''.join(random.choice(string.ascii_uppercase) for _ in range(random.randint(3,10))) # for random name of channel to search 
    urlData = "https://www.googleapis.com/youtube/v3/search?key={}&maxResults={}&part=snippet&type=channel&q={}".format(API_KEY,channels_to_extract,random_name)
    webURL = urllib.request.urlopen(urlData)
    data = webURL.read()
    encoding = webURL.info().get_content_charset('utf-8')
    results = json.loads(data.decode(encoding))
    results_id={}
    if results['pageInfo']["totalResults"]>=channels_to_extract: # may return 0 result because is a random name 
        break                  # when find a result break 

for result in results['items']:
    results_id[result['id']['channelId']]=[result["snippet"]["title"],'https://www.youtube.com/channel/'+result['id']['channelId']] # get id and link of channel for all result

with open("all_info_channels.json","w") as f: # write all info result in a file json
    json.dump(results,f,indent=4)

with open("only_id_channels.json","w") as f: # write only id of channels result in a file json
    json.dump(results_id,f,indent=4)

for channelId in results_id.keys():
    print('Link --> https://www.youtube.com/channel/'+channelId) # link at youtube channel for all result

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Ferro