KeyError:“likeCount”和IndexError:Python中的列表索引超出范围

Posted

技术标签:

【中文标题】KeyError:“likeCount”和IndexError:Python中的列表索引超出范围【英文标题】:KeyError: “likeCount" and IndexError: list index out of range in Python 【发布时间】:2021-08-27 09:22:12 【问题描述】:

我正在使用 Python jupyter notebook 来获取诸如总喜欢计数、不喜欢计数、某些特定 YouTube 频道的所有视频(约 14k 视频)的总观看次数等统计数据。我使用了在 Gitbub 中找到的这段代码。 我能够运行代码直到最后一节 当我尝试运行以下代码行时,我收到错误消息“KeyError:'commentCount'”。与“likeCount”、“dislikeCount”等相同。(错误请打开URL)

for i in range(len(allVideos)):
    i += 1
    title.append((allVideos[i])['snippet']['title'])
    publishedDate.append((allVideos[i])['snippet']['publishedAt'])
    video_description.append((allVideos[i])['snippet']['description'])
    liked.append(int((stats[i])['statistics']['likeCount']))
    disliked.append(int((stats[i])['statistics']['dislikeCount']))
    views.append(int((stats[i])['statistics']['viewCount']))
    comment.append(int((stats[i])['statistics']['commentCount']))
    videoid.append(allVideos[i]['snippet']['resourceId']['videoId'])

KeyError: 'commentCount

我了解此问题可能是由于;当某些视频禁用 cmets、喜欢和不喜欢部分时。我该如何解决这个问题?

我评论了上面提到的一些指标行并重新运行了代码。我最终收到以下错误消息“IndexError: list index out of range”

for i in range(len(allVideos)):
    i += 1
    title.append((allVideos[i])['snippet']['title'])
    #publishedDate.append((allVideos[i])['snippet']['publishedAt'])
    #video_description.append((allVideos[i])['snippet']['description'])
    #liked.append(int((stats[i])['statistics']['likeCount']))
    #disliked.append(int((stats[i])['statistics']['dislikeCount']))
    #views.append(int((stats[i])['statistics']['viewCount']))
    #comment.append(int((stats[i])['statistics']['commentCount']))
    #videoid.append(allVideos[i]['snippet']['resourceId']['videoId'])

IndexError: list index out of range

周围任何可以帮助我的聪明人。我尝试了不同的方法来修复它但不成功??

【问题讨论】:

你为什么要增加计数器i 两次?一次进入循环本身(这在for i in range(n) 中隐含),又一次进入循环体? 您好,我尝试删除“I”并重新运行代码,但错误仍然存​​在。我使用的确切代码在此 github 链接中:github.com/ripulagrawal98/Analytic_Steps/blob/master/… 你可以学习使用for video in allVideos:而不是for i in range(len(allVideos)),这样代码会更简单,更易读。你需要号码然后你可以使用for number, video in enumerate(allVideos): 总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是截图,不是链接到外部门户)有问题(不是评论)。还有其他有用的信息。 你得到list index out of range,因为你使用了i += 1。你不需要它 【参考方案1】:

你得到list index out of range,因为你使用了i += 1。你不需要它。

你也可以学习使用for-loop而不像range(len(...))这样

for video in allVideos:
    title.append( video['snippet']['title'] )
    publishedDate.append( video['snippet']['publishedAt'] )

如果您需要for-loop 中的号码,那么您可以使用enumerate

for number, video in enumerate(allVideos):
    title.append( video['snippet']['title'] )
    publishedDate.append( video['snippet']['publishedAt'] )

    comment.append( int(stats[number]['statistics']['commentCount']) )
    liked.append( int(stats[number]['statistics']['likeCount']) )

但在您的代码中,您可以使用 zip() 来更简单地完成它

for video, stat in zip(allVideos, stats):
    title.append( video['snippet']['title'] )
    publishedDate.append( video['snippet']['publishedAt'] )

    comment.append( int(stat['statistics']['commentCount']) )
    liked.append( int(stat['statistics']['likeCount']) )

如果您首先获得 ['snippet']['statistics'] 并分配给变量,您可以使其更具可读性

for video, stat in zip(allVideos, stats):

    v = video['snippet']

    title.append( v['title'] )
    publishedDate.append( v['publishedAt'] )

    s = stat['statistics']

    comment.append( int(s['commentCount']) )
    liked.append( int(s['likeCount']) )

如果你得到 KeyError 那么你应该使用if/else

    s = stat['statistics']

    if 'commentCount' in s:
        comment.append( int(s['commentCount']) )
    else:
        comment.append( 0 )

    if 'likeCount' in s:
        liked.append( int(s['likeCount']) )
    else:
        liked.append( 0 )

使用.get(key, default value)或更短

    s = stat['statistics']

    comment.append( int(s.get('commentCount', 0)) )
    liked.append( int(s.get('likeCount', 0)) )

完整版:

for video, stat in zip(allVideos, stats):

    v = video['snippet']

    title.append( v['title'] )
    publishedDate.append( v['publishedAt'] )
    video_description.append( v['description'] )
    videoid.append( v['resourceId']['videoId'] )

    s = stat['statistics']

    liked.append( int(s.get('likeCount',0)) )
    disliked.append( int(s.get('dislikeCount',0)) )
    views.append( int(s.get('viewCount',0)) )
    comment.append( int(s.get('commentCount',0)) )

但如果您想将其添加到 DataFrame 中,那么您可以在没有所有这些列表 title 等的情况下使其更简单。

all_rows = []

for video, stat in zip(allVideos, stats):

    v = video['snippet']
    s = stat['statistics']

    row = [
        v['title'],
        v['resourceId']['videoId'],
        v['description'],
        v['publishedAt'],
   
        int(s.get('likeCount',0)),
        int(s.get('dislikeCount',0)),
        int(s.get('viewCount',0)),
        int(s.get('commentCount',0)),
    ]

    all_rows.append(row)

# - after `for`-loop -

df = pd.DataFrame(
         all_rows, 
         columns=['title', 'videoIDS', 'video_description', 'publishedDate', 'likes', 'dislikes', 'views', 'comment']
     )

编辑:

完整的工作代码:

from googleapiclient.discovery import build
import pandas as pd

youTubeApiKey = "AIzaSyCoBcCAxIGkTf5WKxAiXJu48APdyQjqU0I"
youtube = build('youtube', 'v3', developerKey=youTubeApiKey)

snippets = youtube.search().list(part="snippet", type="channel", q="nptelhrd").execute()
print('len(snippets):', len(snippets))

channel_id = snippets['items'][0]['snippet']['channelId']
print('channel_id:', channel_id)

stats = youtube.channels().list(part="statistics", id = channel_id).execute()
print('len(stats):', len(stats))

#status = youtube.channels().list(id=channel_id, part='status').execute()
#print('len(status):', len(status))

content = youtube.channels().list(id=channel_id, part='contentDetails').execute()
print('len(content):', len(content))

upload_id = content['items'][0]['contentDetails']['relatedPlaylists']['uploads']
print('upload_id:', upload_id)

all_videos = []
next_page_token = None
number = 0
while True:
    number +=1 
    print('page', number)
    res = youtube.playlistItems().list(playlistId=upload_id, maxResults=50, part='snippet', pageToken=next_page_token).execute()
    all_videos += res['items']

    next_page_token = res.get('nextPageToken')

    if next_page_token is None:
        break
print('len(all_videos):', len(all_videos))

video_ids = list(map(lambda x: x['snippet']['resourceId']['videoId'], all_videos))
print('len(video_ids):', len(video_ids))

stats = []
for i in range(0, len(video_ids), 40):
  res = youtube.videos().list(id=','.join(video_ids[i:i+40]), part='statistics').execute()
  stats += res['items']
print('len(stats):', len(stats))

all_rows = []

number = 0
for video, stat in zip(all_videos, stats):
    number +=1 
    print('row', number)

    v = video['snippet']
    s = stat['statistics']

    row = [
        v['title'],
        v['resourceId']['videoId'],
        v['description'],
        v['publishedAt'],
   
        int(s.get('likeCount',0)),
        int(s.get('dislikeCount',0)),
        int(s.get('viewCount',0)),
        int(s.get('commentCount',0)),
    ]

    all_rows.append(row)

# - after `for`-loop -

df = pd.DataFrame(all_rows, columns=['title', 'videoIDS', 'video_description', 'publishedDate', 'likes', 'dislikes', 'views', 'comment'])

print(df.head())

结果:

                                               title     videoIDS  ... views comment
0  NPTEL Awareness workshop in association with P...  TCMQ2NEEiRo  ...  7282       0
1                            Cayley-Hamilton theorem  WROFJ15hk00  ...  3308       4
2  Recap of matrix norms and Levy-Desplanques the...  WsO_s8dNfVI  ...   675       1
3                  Convergent matrices, Banach lemma  PVGeabmeLDQ  ...   676       2
4                  Schur's triangularization theorem  UbDwzSnS0Y0  ...   436       0

[5 rows x 8 columns]

【讨论】:

下面是我收到的错误信息 ---------------------------------- ----------------------------------------- ??TypeError Traceback(最近一次调用最后一次) 中的 ----> 1 用于视频,枚举中的 stat(allVideos, stats): 2 3 v = video['sn-p'] 4 5 title.append( v['title'] ) TypeError: 'list' 对象不能被解释为整数 出现错误 - 应该是 zip() 而不是 enumerate() 能否为您提供我前面提到的部分的完整代码...我将运行相同的代码并对其进行测试 同时我正在创建完整的工作代码,现在这个代码正在回答中。 哇哇哇哇......你简直太棒了,这段代码效率更高......这正是我想要的。有没有办法给解决方案评分...显然 5stars 快速转身..再次感谢,祝您有美好的一天

以上是关于KeyError:“likeCount”和IndexError:Python中的列表索引超出范围的主要内容,如果未能解决你的问题,请参考以下文章

执行朴素贝叶斯和决策树分类时出现 KeyError

Django:request.GET 和 KeyError

django_heroku.settings(locals()) KeyError: 'MIDDLEWARE' 和 'MIDDLEWARE_CLASSES'

python里出现keyerror 怎么解决

ValueError:错误的项目数通过 47,位置意味着 1 和 KeyError:'size'

KeyError:使用 Python Instagram API 客户端的“数据”