如何在PRAW的subreddit中列出顶级评论?

Ray*_*lez 2 python reddit praw

我需要一直抓住subreddit中的热门评论.

我已经尝试抓住所有提交内容并迭代它们,但不幸的是,您可以获得的帖子数量限制为1000.

我尝试过使用Subreddit.get_comments,但它只返回25条评论.

所以我正在寻找解决方法.

你能帮我吗?

Pok*_*u22 7

可以使用set get_comments参数来获取所有可用的注释.(默认情况下,它使用帐户的金额,通常为25).(用于包括的参数包括).limitNoneget_commentsget_contentlimit

但是,这可能不会做你想要的 - get_comments(或更具体地说/r/subreddit/comments)只提供新评论或新的镀金评论列表,而不是最高评论.而且由于get_comments还有1000条评论,因此您无法构建完整的评论列表.

所以你真正想要的是原始算法 - 得到最高提交的列表,然后是那些的最高评论.这不是一个完美的系统(一个低分的帖子可能实际上有一个高度评价的评论),但它是最好的.

这是一些代码:

import praw

r = praw.Reddit(user_agent='top_comment_test')
subreddit = r.get_subreddit('opensource')
top = subreddit.get_top(params={'t': 'all'}, limit=25) # For a more potentially accurate set of top comments, increase the limit (but it'll take longer)
all_comments = []
for submission in top: 
    submission_comments = praw.helpers.flatten_tree(submission.comments)
    #don't include non comment objects such as "morecomments"
    real_comments = [comment for comment in submission_comments if isinstance(comment, praw.objects.Comment)]
    all_comments += real_comments

all_comments.sort(key=lambda comment: comment.score, reverse=True)

top_comments = all_comments[:25] #top 25 comments

print top_comments
Run Code Online (Sandbox Code Playgroud)