java 查看对 YouTube 视频的所有评论

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35441093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:09:17  来源:igfitidea点击:

View all comments on a YouTube video

javayoutube

提问by Walshy

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.

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

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

我已经尝试过 all_comments 而不是在 URL 中观看,但它仍然没有显示所有评论并重定向以再次观看。

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.

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

If anyone knows how to do this please tell me.

如果有人知道如何做到这一点,请告诉我。

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

我已经为任何能给我一个很好的答案的人添加了 50 代表赏金。

采纳答案by Michael Cheremuhin

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();
    }
}

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

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



Update

更新

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

无法获得所有评论的情况也可能是配额限制造成的(至少我遇到过):

  • units/day50,000,000
  • units/100seconds/user300,000
  • 单位/50,000,000
  • 单位/ 100秒/用户300,000

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/userquota.

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

回答by samsamara

try this it can download all the comments for a given video which i have tested.

试试这个它可以下载我测试过的给定视频的所有评论。

https://github.com/egbertbouman/youtube-comment-downloader

https://github.com/egbertbouman/youtube-comment-downloader

python downloader.py --youtubeid YcZkCnPs45s --output OUT 
Downloading Youtube comments for video: YcZkCnPs45s 
Downloaded 1170 comment(s) 
Done!

output is in the JSON format:

输出采用 JSON 格式:

{
  "text": "+Tony Northrup many thanks for the prompt reply - I'll try that.?",
  "time": "1 day ago",
  "cid": "z13nfbog0ovqyntk322txzjamuensvpch.1455717946638546"
}