2

I'm trying to get the view count for a list of videos from a channel. I've written a function and when I try to run it with just 'video_id', 'title' & 'published date' I get the output. However, when I want the view count or anything from statistics part of API, then it is giving a Key Error.

Here's the code:

def get_video_details(youtube, video_ids):
    
    all_video_stats = []
    
    for i in range(0, len(video_ids), 50):
        request = youtube.videos().list(
                        part='snippet,statistics',
                        id = ','.join(video_ids[i:i+50]))
        response = request.execute()
        
        for video in response['items']:
            video_stats = dict(
                            Video_id = video['id'],
                            Title = video['snippet']['title'],
                            Published_date = video['snippet']['publishedAt'],
                            Views = video['statistics']['viewCount'])

            all_video_stats.append(video_stats)

    return all_video_stats

get_video_details(youtube, video_ids)

And this is the error message:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_18748/3337790216.py in <module>
----> 1 get_video_details(youtube, video_ids)

~\AppData\Local\Temp/ipykernel_18748/1715852978.py in get_video_details(youtube, video_ids)
     14                             Title = video['snippet']['title'],
     15                             Published_date = video['snippet']['publishedAt'],
---> 16                             Views = video['statistics']['viewCount'])
     17 
     18             all_video_stats.append(video_stats)

KeyError: 'viewCount'

I was referencing this Youtube video to write my code.

Thanks in advance.

4
  • Did you try to check what keys are present in the result? Did you try reading the API documentation, in order to understand what should be present? Commented Mar 17, 2022 at 4:53
  • Did you do a null check? I tested it and its working fine for me if a viewCount is there. Commented Mar 17, 2022 at 7:14
  • As @DalmTo said, it's not impossible that the value of viewCount is null, as far as I remember having worked with it, it can be null or any integer (including 0) Commented Mar 17, 2022 at 9:21
  • When I'm trying to run the code for a single video, I'm getting the answer. But I want the solution for a list of videos and there are so many videos. Commented Mar 17, 2022 at 22:40

1 Answer 1

1

I got it.

I had to use .get() to avoid the KeyErrors. It will return None for KeyErrors.

Replaced this code to get the solution.

Views = video['statistics'].get('viewCount')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.