获取 KeyError:“viewCount”用于在 Python 中使用 Youtube API

发布于 2025-01-14 23:00:45 字数 1780 浏览 3 评论 0原文

我正在尝试获取某个频道的视频列表的观看次数。我已经编写了一个函数,当我尝试仅使用“video_id”、“title”和“”来运行它时“发布日期”我得到输出。但是,当我想要查看次数或 API 统计部分的任何内容时,它会给出关键错误。

这是代码:

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)

这是错误消息:

---------------------------------------------------------------------------
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'

我引用了这个 Youtube 视频来编写我的代码。

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声情话 2025-01-21 23:00:45

我得到了它。

我必须使用 .get() 来避免 KeyErrors。对于 KeyErrors,它将返回 None。

替换此代码以获得解决方案。

Views = video['statistics'].get('viewCount')

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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文