我如何使用 PRAW 列出 subreddit 中的热门评论?

How can I make a list of the top comments in a subreddit with PRAW?

我需要随时获取 subreddit 中的热门评论。

我尝试抓取所有提交的内容,并遍历它们,但不幸的是,您可以获得的帖子数量限制为 1000。

我试过使用 Subreddit.get_comments,但 returns 只有 25 条评论。

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

你能帮帮我吗?

可以使用get_comments with a parameter of limit set to None to get all available comments. (By default, it uses the amount for the account, which is usually 25). (The parameters that are used for get_comments include the ones for get_content,包括limit)。

但是,这可能不会如您所愿 – get_comments(或更具体地说 /r/subreddit/comments)仅提供新评论或新镀金评论的列表,而不是热门评论。由于 get_comments 也限制为 1000 条评论,因此您将无法构建完整的热门评论列表。

所以你真正想要的是原始算法——获取最热门提交的列表,然后是那些最热门评论。这不是完美的系统(低分 post 实际上可能有高投票的评论),但它是最好的系统。

这是一些代码:

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