查看YouTube视频的所有评论 [英] View all comments on a YouTube video

查看:233
本文介绍了查看YouTube视频的所有评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java程序获取YouTube视频的所有评论。我不能得到它们,因为它有显示更多而不是所有评论。我正在寻找一种方法来获取我可以通过的所有评论或评论页面。我有一个视频ID和东西,只需要评论。

I am trying to get all the comments on a YouTube video using a Java program. I cannot get them though as it has the "Show More" instead of all the comments. I'm looking for a way to get all the comments or pages of comments that I can go through. I have a video id and things, just need the comments.

我已尝试过all_comments而不是在网页中观看,但它仍未显示所有评论并重定向到再看一遍。

I have tried all_comments instead of watch in the URL but it doesn't show all comments still and redirects to watch again.

我也看了一下YouTube api,只能找到如何用他们的id获取评论,但我需要从视频ID中获取所有评论。

I have also looked at the YouTube api and can only find how to get comments with their id but I need to get all comments from a video id.

如果有人知道怎么做,请告诉我。

If anyone knows how to do this please tell me.

我已为任何人增加了50个代表奖金给我一个很好的答案。

I have added a 50 rep bounty for whoever can give me a good answer to this.

推荐答案

您需要获取视频的评论主题列表请求,然后使用下一步向前滚动来自上一个响应的页面标记:

You need to get comment threads list request for your video and then scroll forward using next page token from the last response:

private static int counter = 0;
private static YouTube youtube;

public static void main(String[] args) throws Exception {
    // For Auth details consider:
    // https://github.com/youtube/api-samples/blob/master/java/src/main/java/com/google/api/services/samples/youtube/cmdline/Auth.java
    // Also don't forget secrets https://github.com/youtube/api-samples/blob/master/java/src/main/resources/client_secrets.json
    List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.force-ssl");
    Credential credential = Auth.authorize(scopes, "commentthreads");
    youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).build();

    String videoId = "video_id";

    // Get video comments threads
    CommentThreadListResponse commentsPage = prepareListRequest(videoId).execute();

    while (true) {
        handleCommentsThreads(commentsPage.getItems());

        String nextPageToken = commentsPage.getNextPageToken();
        if (nextPageToken == null)
            break;

        // Get next page of video comments threads
        commentsPage = prepareListRequest(videoId).setPageToken(nextPageToken).execute();
    }

    System.out.println("Total: " + counter);
}

private static YouTube.CommentThreads.List prepareListRequest(String videoId) throws Exception {

    return youtube.commentThreads()
                  .list("snippet,replies")
                  .setVideoId(videoId)
                  .setMaxResults(100L)
                  .setModerationStatus("published")
                  .setTextFormat("plainText");
}

private static void handleCommentsThreads(List<CommentThread> commentThreads) {

    for (CommentThread commentThread : commentThreads) {
        List<Comment> comments = Lists.newArrayList();
        comments.add(commentThread.getSnippet().getTopLevelComment());

        CommentThreadReplies replies = commentThread.getReplies();
        if (replies != null)
            comments.addAll(replies.getComments());

        System.out.println("Found " + comments.size() + " comments.");

        // Do your comments logic here
        counter += comments.size();
    }
}

考虑 api-samples ,如果您需要示例骨架项目。

Consider api-samples, if you need a sample skeleton project.

更新

无法获得所有评论的情况也可以由配额限制(至少我遇到过)引起:

The situation when you can't get all the comments can be also caused by the quota limits (at least I faced it):


  • 单位/ 50,000,000

  • 单位/ 100秒/用户 300,000

  • units/day 50,000,000
  • units/100seconds/user 300,000

这不是java,python,js或其他特定于语言的规则。如果您想要超过配额,则无法尝试申请更高的配额。但是,我会从控制吞吐量开始。很容易超过 100秒/用户配额。

This is not a java, python, js, or whatever language specific rules. If you want to get above the quota, you cant try to apply for higher quota. Though, I would start from controlling your throughput. It's very easy to get above the 100seconds/user quota.

这篇关于查看YouTube视频的所有评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆