2

我需要从所有时间获取 subreddit 中的热门评论。

我已经尝试抓取所有提交的内容,并对其进行迭代,但不幸的是,您可以获得的帖子数量仅限于 1000 个。

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

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

你能帮我吗?

4

1 回答 1

7

可以使用setget_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
于 2015-09-12T00:21:00.410 回答