Youtube评论提取API V3

2024-04-26 17:56:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用这段代码,但它的工作方式与API v2不同,请提供用于提取youtube评论的代码。在

import gdata.youtube.service
yts = gdata.youtube.service.YouTubeService()
ytfeed = yts.GetYouTubeVideoCommentFeed(video_id="pXhcPJK5cMc")
comments = [comment.content.text for comment in ytfeed.entry]

Tags: 代码importapiidyoutubevideoservice方式
1条回答
网友
1楼 · 发布于 2024-04-26 17:56:18

要使用YouTube API v.3提取YouTube视频评论,需要以下Python代码:

def get_video_comments(service, **kwargs):
    comments = []
    results = service.commentThreads().list(**kwargs).execute()

    while results:
        for item in results['items']:
            comment = item['snippet']['topLevelComment']['snippet']['textDisplay']
            comments.append(comment)

        # Check if another page exists
        if 'nextPageToken' in results:
            kwargs['pageToken'] = results['nextPageToken']
            results = service.commentThreads().list(**kwargs).execute()
        else:
            break

    return comments

有关此代码的详细信息,以及如何使用YouTube API使用关键字搜索视频,然后将注释保存到CSV文件中,可以查看此教程:https://python.gotrained.com/youtube-api-extracting-comments/

相关问题 更多 >