gpt4 book ai didi

java - 如何使用Java检索超过25个YouTube评论

转载 作者:行者123 更新时间:2023-12-03 06:32:47 26 4
gpt4 key购买 nike

      YouTubeService service = new YouTubeService("MyApp");
String videoEntryUrl = "http://gdata.youtube.com/feeds/api/videos/nU9dinwMyHo";
VideoEntry videoEntry = service.getEntry(new URL(videoEntryUrl), VideoEntry.class);
String commentUrl = videoEntry.getComments().getFeedLink().getHref();

CommentFeed commentFeed = service.getFeed(new URL(commentUrl), CommentFeed.class);
for(CommentEntry comment : commentFeed.getEntries()) {
System.out.println(comment.getPlainTextContent());

这是我的代码,用于检索特定视频的youtube评论。
我可以使用此代码检索约25条评论,但是如何从视频中检索所有评论?

最佳答案

我认为是这样的:

// Get a video entry
String str = "http://gdata.youtube.com/feeds/api/videos/" + videoId;
YouTubeQuery youtubeQuery = new YouTubeQuery(new URL(str));
String videoEntryUrl = youtubeQuery.getUrl().toString();
VideoEntry videoEntry = service.getEntry(new URL(videoEntryUrl),VideoEntry.class);

// Get the comments url for this video
if (videoEntry.getComments() != null) {
String commentUrl = videoEntry.getComments().getFeedLink().getHref();
System.out.println(commentUrl);

// Get the comment feed; use a new query
YouTubeQuery youtubeQuery = new YouTubeQuery(new URL(commentUrl);
youtubeQuery.setMaxResults(50);


youtubeQuery.setStartIndex(1);
String commentUrlFeed = youtubeQuery.getUrl().toString();
CommentFeed commentFeed = service.getFeed(new URL(commentUrlFeed),CommentFeed.class);
// The response should contain an url for the next feed, if any, already with an updated start-index.

for (int i = 0; i < commentFeed.getEntries().size()
&& commentFeed.getEntries().get(i) != null; i++) {

String author=commentFeed.getEntries().get(i).getAuthors().get(0).getUri().substring(41)
String commentId=commentFeed.getEntries().get(i).getId().substring(47);
String comment=commentFeed.getEntries().get(i).getPlainTextContent();
}
}
// Loop thru next comment feed call, if more can be expected.
// Use updated url from the response or set start-index = start-index + max-results.
}

关于java - 如何使用Java检索超过25个YouTube评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18527048/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com