youtube数据api评论分页

时间:2009-12-14 15:17:40

标签: python youtube youtube-api

我在迭代youtube视频上的所有评论的语法上苦苦挣扎。我正在使用python并且在GetYouTubeVideoCommentFeed()函数上找到了很少的文档。

我真正想要做的是搜索视频的所有评论以获取单词的实例并增加计数器(最终将打印出评论)。它对返回的25个结果起作用,但我需要访问其余的注释。

import gdata.youtube
import gdata.youtube.service

video_id = 'hMnk7lh9M3o'
yt_service = gdata.youtube.service.YouTubeService()    
comment_feed = yt_service.GetYouTubeVideoCommentFeed(video_id=video_id)
for comment_entry in comment_feed.entry:
 comment = comment_entry.content.text
 if comment.find('hi') != -1:
  counter = counter + 1

print "hi: "
print counter

除了start_index之外,我尝试设置GetYouTubeVideoCommentFeed()的{​​{1}},但它不喜欢这样。

我有什么遗失的吗?

谢谢! 史蒂夫

2 个答案:

答案 0 :(得分:5)

以下是相同的代码段:

# Comment feed URL
comment_feed_url = "http://gdata.youtube.com/feeds/api/videos/%s/comments"

''' Get the comment feed of a video given a video_id'''        
def WriteCommentFeed(video_id, data_file):  
    url = comment_feed_url % video_id
    comment_feed = yt_service.GetYouTubeVideoCommentFeed(uri=url)

    try:
        while comment_feed:

            for comment_entry in comment_feed.entry:
                print comment_entry.id.text
                print comment_entry.author[0].name.text
                print comment_entry.title.text
                print comment_entry.published.text
                print comment_entry.updated.text
                print comment_entry.content.text

            comment_feed = yt_service.Query(comment_feed.GetNextLink().href) 

    except:
            pass

答案 1 :(得分:1)

了解如何做到这一点。您可以传递一个URL,而不是将video_id传递给GetYouTubeVideoCommentFeed函数。您可以通过更改URL参数来迭代注释。

但必须有API限制;我只能访问视频的最后1000条评论。