2009-12-14 122 views
4

我正在努力一点点的语法迭代通过YouTube视频上的所有评论。我正在使用python,并且在GetYouTubeVideoCommentFeed()函数上找到了一些文档。youtube数据API评论分页

我真正想做的是搜索一个词的实例的视频的所有评论,并增加一个计数器(最终评论将打印出来)。它对返回的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 

我试图设置的GetYouTubeVideoCommentFeed()start_index除了video_id但它并没有这样的。

有什么我失踪了吗?

谢谢! Steve

回答

1

发现了如何去做。您可以将URL传递给GetYouTubeVideoCommentFeed函数,而不是将video_id传递给GetYouTubeVideoCommentFeed函数。您可以通过更改URL参数来遍历评论。

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

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