import requests import pandas as pd # Google API key key = 'your-key' # ID of the video you want to analyze (get it from YouTube url) video_id = 'wSY-KRMpzhM' # initialize empty list of users user_list = [] # get the first set of comments first_response = requests.get('https://www.googleapis.com/youtube/v3/commentThreads?key=' + key + '&textFormat=plainText&part=snippet&videoId=' + video_id + '&maxResults=100') data1 = first_response.json() d1 = data1.get('items') # get the commenter's channel url for the first set of comments for snippet in d1: url = snippet.get('snippet').get('topLevelComment').get('snippet').get('authorChannelUrl') user_list.append(url) # get next page token if there are more than 100 comments nextPageToken = data1.get('nextPageToken') # get the rest of the comments while nextPageToken: next_response = requests.get('https://www.googleapis.com/youtube/v3/commentThreads?key=' + key + '&textFormat=plainText&part=snippet&videoId=' + video_id + '&maxResults=100&pageToken=' + nextPageToken) data2 = next_response.json() #print data2 d2 = data2.get('items') nextPageToken = data2.get('nextPageToken') for snippet in d2: url = snippet.get('snippet').get('topLevelComment').get('snippet').get('authorChannelUrl') if url: user_list.append(url) import pprint pprint.pprint(user_list) print len(user_list),' users commented.' # initialize empty list for the data user_list_subs = [] #loop through all channels who commented on the video and get their statistics for channel in user_list: #get the channel id id = channel[-24:] print id #get channel statistics user_response_stats = requests.get('https://www.googleapis.com/youtube/v3/channels?part=statistics&key=' + key + '&id=' + id) user_data = user_response_stats.json() # get subscriber count sub_count = user_data.get('items')[0].get('statistics').get('subscriberCount') sub_count = int(sub_count) print sub_count # get the channel's view count view_count = user_data.get('items')[0].get('statistics').get('viewCount') view_count = int(view_count) print view_count # get the channel's video count (how many videos it uploaded) video_count = user_data.get('items')[0].get('statistics').get('videoCount') video_count = int(video_count) print video_count # get channel metadata, e.g. title and custom url user_response_snippet = requests.get('https://www.googleapis.com/youtube/v3/channels?part=snippet&key=' + key + '&id=' + id) user_data = user_response_snippet.json() title = user_data.get('items')[0].get('snippet').get('title') custom_url = user_data.get('items')[0].get('snippet').get('customUrl') print title #add all data to the list user_list_subs.append([channel,title,sub_count,view_count,video_count,custom_url]) pprint.pprint(user_list_subs) #Convert user_list_subs to dataframe import pandas as pd labels = ['channel','title','subscriber_count','view_count','video_count','custom_url'] df = pd.DataFrame.from_records(user_list_subs, columns = labels) # sort the data by number of subscribers df = df.sort_values(by=['subscriber_count'],ascending=False) print df # save the sorted data frame to a csv df.to_csv('parachute-young-march28.csv',index=False, encoding='utf-8')