My code allows to call data from Youtube API and put them into list to form a datafram.However, when the API cannot access the 'items' it returns error. In order to avoid errors, I need to append 'n/a' or 'null' and pass when the API cannot access certain items.
def get_channel_stats(youtube, channel_ids):all_data = []for i in range(0, len(channel_ids), 50): request = youtube.channels().list( part='snippet,contentDetails,statistics', id = ','.join(channel_ids[i:i+50])) response = request.execute() for i in range(len(response['items'])): data = dict(channel_name = response['items'][i]['snippet']['title'], published_date = response['items'][i]['snippet']['publishedAt'], subscribers = response['items'][i]['statistics']['subscriberCount'], views = response['items'][i]['statistics']['viewCount'], total_videos = response['items'][i]['statistics']['videoCount'], playlist_id = response['items'][i]['contentDetails']['relatedPlaylists']['uploads'], retrieved_date = date.today().strftime('%Y/%m/%d') ) all_data.append(data)return all_dataAny directions or advice would be very helpful.
Thank you in advanced.