Youtube channel Analysis

Hi I am doing youtube channel analysis for zeemusic company…but facing some issues in my coding.

I am attaching my notebook which I am working on my desktop.

I am getting key error for ‘items’ in Line 8. I dont know what issue is there. I tried all possible changes i could but unable to rectify it. Please help me

  1. Importing Pandas library for saving data in a dataframe and exporting it in csv file

import pandas as pd

Importing requests, it’s a Python HTTP library for making HTTP requests

import requests

Importing JSON library to save retrieved data in json format

import json

  1. Your API key

api_key = ‘AIzaSyDUXNTo69KGHru_2fIBzZ3VkBwB-8le6ys’ # Replace this key with your API key

  1. channel_ID of Zee Music Company

Channel_Id = “UCFFbwnve3yF62-tVXkTyHqg”

  1. For channel’s basic statistics

url1 = f"https://www.googleapis.com/youtube/v3/channels?part=statistics&key={api_key}&id={Channel_Id}"
print(url1)
channel_info = requests.get(url1)
json_data1 = json.loads(channel_info.text)

  1. channel_info

Out[5]:

<Response [200]>

json_data1
{‘kind’: ‘youtube#channelListResponse’,
‘etag’: ‘3bqoDAomDypE15cAcOBMyuECGDs’,
‘pageInfo’: {‘totalResults’: 1, ‘resultsPerPage’: 5},
‘items’: [{‘kind’: ‘youtube#channel’,
‘etag’: ‘jdS18X45mDKuEV8zEi-NPfjGQ7A’,
‘id’: ‘UCFFbwnve3yF62-tVXkTyHqg’,
‘statistics’: {‘viewCount’: ‘42496332175’,
‘subscriberCount’: ‘79100000’,
‘hiddenSubscriberCount’: False,
‘videoCount’: ‘5897’}}]}

channel_subscribers = int(json_data1[‘items’][0][‘statistics’][‘subscriberCount’]);
channel_videos = int(json_data1[‘items’][0][‘statistics’][‘videoCount’]);

print('Total Subsribers = ‘,channel_subscribers,’\nTotal videos on this channel = ',channel_videos)
Total Subsribers = 79100000
Total videos on this channel = 5897

8 limit = 15 # how many pages of information you want
video_Ids = []
nextPageToken ="" # used here to get page with unrepeated content, for 0th iteration let it be null
for i in range(limit):
url = f"https://www.googleapis.com/youtube/v3/search?key={api_key}&part=snippet&channelId={Channel_Id}&maxResults=50&pageToken={nextPageToken}"
data = json.loads(requests.get(url).text)
for item in data[‘items’]:
video_Id = item[‘id’][‘videoId’]
video_Ids.append(video_Id) # Storing video Ids for extracting videos information
nextPageToken = data[‘nextPageToken’] # to collect videos from the next page

output :

KeyError Traceback (most recent call last)
in
5 url = f"https://www.googleapis.com/youtube/v3/search?key={api_key}&part=snippet&channelId={Channel_Id}&maxResults=50&pageToken={nextPageToken}"
6 data = json.loads(requests.get(url).text)
----> 7 for item in data[‘items’]:
8 video_Id = item[‘id’][‘videoId’]
9 video_Ids.append(video_Id) # Storing video Ids for extracting videos information

KeyError: ‘items’

please help me rectify this error. I am badly stuck in this…Your help is really appreciated.
Thanks in advance :slight_smile:

Hey … please help me with the coding error I am stuck …

Your help is really appreciated… notebook is attached above in my previous message…

Can you indivisually show what does data[‘items’] store?

Hi @himani007 ,

thanks for replying. I thought no one would reply to it. I am really stuck on this since two weeks. I have done lot of googling but unable to understand this part. There was a project done on Youtube analysis on t-series on jovian earlier… I took reference from there but still unable to get it.

I am really at the beginner stage of understand. so please bear with me. And please help me with this coding. :slight_smile:

I have printed the data[‘items’]…now its giving error on ‘videoId’ key…I need line 8 and 9 to work so that i can get my data uploaded for the current url1 with limit15…
I dont understand where I am going wrong

Try,

video_Id = item['id']['videoId'][0]

@utkarsh736
It’s not working…it is still giving
keyError ‘videoId’
Indicating the line -
VideoId = item[‘Id’][‘videoId’]

Maybe try,

video_Id = item['id'][1]['videoId']

I am also not much experienced in JSON/web-scrapping, but I think the problem is
item is a dictionary in which
'id' is a list of dictionaries in which
'videoId' is a dictionary

So, to access the content/value you need to access it like a list.

If this doesn’t work you can next try

video_Id = item['id'][1]['videoId'][0]

@utkarsh736 it doesn’t work…

Hey, I tried your code and the problem seems to be that not all the items in the dictionary items have a key videoId.

Only the items where 'kind' is video have videoId key, So you need to loop through the items where kind is video

This code seems to work for me:

 for item in data['items']: 
        i = item['id']
        if i['kind']=='youtube#video':  # Condition to loop through the video-kind
          video_Id = i['videoId']  

        video_Ids.append(video_Id)  # Storing video Ids for extracting videos information
nextPageToken = data['nextPageToken'] # to collect videos from the next page

You can change the variables as per your wish.

Also, you can check other kinds by doing

item['id']['kind']

@utkarsh736

Thank you so much …
I understand your point now…

I hope it works…:+1::blush: