检索Youtube评论回复

时间:2016-07-17 02:27:10

标签: youtube-api comments

我正在尝试从youtube视频中提取评论。我能够检索评论(https://developers.google.com/youtube/v3/docs/commentThreads/list#try-it),但我无法确定如何检索各个评论的回复。我查看了Youtube API文档,但无法确切地指出如何检索评论回复。如果有可能,任何人都可以告诉我吗?如果是,我该怎么做。感谢。

2 个答案:

答案 0 :(得分:1)

从此documentation,您可以使用parentId参数,该参数指定应检索回复的注释的ID。但请注意,YouTube目前仅支持针对顶级评论的回复,并且可能会在将来支持对回复的回复。您可以使用comments.list方法检索评论回复。

示例:

//Call the YouTube Data API's comments.list method  to retrieve existing comment replies.

V3CommentListResponse commentsListResponse = youtube.comments().list("snippet")
         .setParentId(parentId).setTextFormat("plainText").execute();
List<Comment> comments = commentsListResponse.getItems();

if (comments.isEmpty()) {
    System.out.println("Can't get comment replies.");
} else {
    // Print information from the API response.
    System.out.println("\n===========Returned Comment Replies============\n");  

    for (Comment commentReply : comments) {
         snippet = commentReply.getSnippet();
         System.out.println("  - Author: " + snippet.getAuthorDisplayName());
         System.out.println("  - Comment: " + snippet.getTextDisplay());
         System.out.println("\n---------------\n");
    }

    Comment firstCommentReply = comments.get(0);
    firstCommentReply.getSnippet().setTextOriginal("updated");
    Comment commentUpdateResponse = youtube.comments()
            .update("snippet", firstCommentReply).execute();
    // Print information from the API response.
    System.out.println("\n============Updated Video Comment===============\n");
         snippet = commentUpdateResponse.getSnippet();
         System.out.println("  - Author: " + snippet.getAuthorDisplayName());
         System.out.println("  - Comment: " + snippet.getTextDisplay());
         System.out.println("\n--------------------------------\n");

检查此相关thread

答案 1 :(得分:0)

根据 API Documentation of YouTube,您可以通过将值为 part 的参数 snippet,replies 添加到 commentThreads 端点来检索评论和相关回复,如下所示:

https://www.googleapis.com/youtube/v3/commentThreads?part=snippet,replies&videoId=[VIDEO_ID]&key=[YOUR_YOUTUBE_API_KEY]

上面的示例包括视频 ID 和 API 密钥。