我正在尝试使用 youtube API 抓取给定 videoId 的评论。 但爬取的评论数量少于其实际数量。 你对此有什么想法吗?我的代码如下。
from googleapiclient.discovery import build
from typing import List
def get_comments(api, video_id: str, fields: str)-> List[List[str]]:
comments = list()
response = api.commentThreads().list(part='snippet', fields=fields, videoId=video_id, maxResults=50).execute()
all_comment_crawled = True
while all_comment_crawled:
for item in response['items']:
comment = item['snippet']['topLevelComment']['snippet']
comments.append([comment['textOriginal'], comment['likeCount']])
if 'nextPageToken' in response:
response = api.commentThreads().list(part='snippet', videoId=video_id, fields=fields, pageToken=response['nextPageToken'], maxResults=50).execute()
else:
all_comment_crawled = False
return comments
api_key = "MY_API_KEY"
api_obj = build('youtube', 'v3', developerKey=api_key)
video_id = 'fgSvGLxanCo'
fields = 'items(snippet(totalReplyCount, topLevelComment(snippet(textOriginal, likeCount)))), nextPageToken'
comments = get_comments(api_obj, video_id, fields)
print(len(comments)) # returns 1,945 actually is over 2,000
在 YouTube 视频上列出评论时有一个陷阱(我宣布另一个很容易陷入的陷阱):