KeyError: 'commentCount' 在 Python 中使用 Youtube API
Posted
技术标签:
【中文标题】KeyError: \'commentCount\' 在 Python 中使用 Youtube API【英文标题】:KeyError: 'commentCount' using Youtube API in PythonKeyError: 'commentCount' 在 Python 中使用 Youtube API 【发布时间】:2022-01-03 07:15:45 【问题描述】:我使用这个 python 代码从 Youtube API v3 获取数据。
首先构建了一个函数来获取观看次数、喜欢次数、不喜欢次数和评论次数,然后是另一个大块,它本身就是一个获取“视频 id”等信息的函数,最后它将所有内容放入 pandas 数据帧中。
它适用于某些 YouTube 频道,但其他一些频道无法获取 cmets 的数量。我在我的 Python Notebook 中遇到了这个错误消息:
KeyError Traceback (most recent call last)
<ipython-input-46-de3cf6032d33> in <module>()
5 df2 = pd.DataFrame(columns=['video_id','video_title','upload_date','view_count','like_count','dislike_count','comment_count'])
6
----> 7 df2 = get_videos(df2)
1 frames
<ipython-input-44-90ae6e5b0155> in get_video_details(video_id)
8 like_count = response_video_stats['items'][0]['statistics']['likeCount']
9 dislike_count = response_video_stats['items'][0]['statistics']['dislikeCount']
---> 10 comment_count = response_video_stats['items'][0]['statistics']['commentCount']
11
12 #return view_count
KeyError: 'commentCount'
我想if else
声明可以解决问题,但无论如何都不是最佳选择。
这是完整的代码:
# Import libraries
import requests
import pandas as pd
import time
# Keys
API_KEY = 'xxx'
CHANNEL_ID = 'xxx'
def get_video_details(video_id):
#collecting view, like, dislike, comment counts
url_video_stats = 'https://www.googleapis.com/youtube/v3/videos?id='+video_id+'&part=statistics&key='+API_KEY
response_video_stats = requests.get(url_video_stats).json()
view_count = response_video_stats['items'][0]['statistics']['viewCount']
like_count = response_video_stats['items'][0]['statistics']['likeCount']
dislike_count = response_video_stats['items'][0]['statistics']['dislikeCount']
comment_count = response_video_stats['items'][0]['statistics']['commentCount']
return view_count, like_count, dislike_count, comment_count
def get_videos(df):
pageToken = ''
while 1:
url = 'https://www.googleapis.com/youtube/v3/search?key='+API_KEY+'&channelId='+CHANNEL_ID+'&part=snippet,id&order=date&maxResults=10000&'+pageToken
response = requests.get(url).json()
time.sleep(1) #give it a second before starting the for loop
for video in response['items']:
if video['id']['kind'] == "youtube#video":
video_id = video['id']['videoId']
video_title = video['snippet']['title']
video_title = str(video_title).replace('&','')
upload_date = video['snippet']['publishedAt']
upload_date = str(upload_date).split("T")[0]
#view_count = get_video_details(video_id)
view_count, like_count, dislike_count, comment_count = get_video_details(video_id)
df = df.append('video_id':video_id,
'video_title':video_title,
'upload_date':upload_date,
'view_count':view_count,
'like_count':like_count,
'dislike_count':dislike_count,
'comment_count':comment_count,
ignore_index=True)
try:
if response['nextPageToken'] != None: #if none, it means it reached the last page and break out of it
pageToken = 'pageToken=' + response['nextPageToken']
except:
break
return df
#build our dataframe
df2 = pd.DataFrame(columns=['video_id','video_title','upload_date','view_count','like_count','dislike_count','comment_count'])
df2 = get_videos(df2)
【问题讨论】:
.get('commentCount', 0)
?
我把它放在 Try except 中就可以了
【参考方案1】:
第一个函数中的try except
确实可以很好地解决问题
def get_video_details(video_id):
#collecting view, like, dislike, comment counts
url_video_stats = 'https://www.googleapis.com/youtube/v3/videos?id='+video_id+'&part=statistics&key='+API_KEY
response_video_stats = requests.get(url_video_stats).json()
#view_count = response_video_stats['items'][0]['statistics']['viewCount']
#like_count = response_video_stats['items'][0]['statistics']['likeCount']
#dislike_count = response_video_stats['items'][0]['statistics']['dislikeCount']
#comment_count = response_video_stats['items'][0]['statistics']['commentCount']
view_count = response_video_stats['items'][0]['statistics']['viewCount']
try:
like_count = response_video_stats['items'][0]['statistics']['likeCount']
except KeyError:
like_count = response_video_stats.get('likeCount', 0)
try:
dislike_count = response_video_stats['items'][0]['statistics']['dislikeCount']
except:
dislike_count = response_video_stats.get('dislikeCount', 0)
try:
comment_count = response_video_stats['items'][0]['statistics']['commentCount']
except:
comment_count = response_video_stats.get('commentCount', 0)
return view_count, like_count, dislike_count, comment_count
【讨论】:
以上是关于KeyError: 'commentCount' 在 Python 中使用 Youtube API的主要内容,如果未能解决你的问题,请参考以下文章
Pytorch:“KeyError:在 DataLoader 工作进程 0 中捕获 KeyError。”
python3 日志检索异常抛出异常 raise KeyError(key),KeyError: 'formatters'