【问题标题】:How can I make a list of the top comments in a subreddit with PRAW?如何使用 PRAW 列出子版块中的热门评论?
【发布时间】:2015-12-26 23:29:15
【问题描述】:

我需要一直在 subreddit 中获取顶级 cmets。

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

我尝试过使用Subreddit.get_comments,但它只返回 25 个 cmets。

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

你能帮帮我吗?

【问题讨论】:

    标签: python reddit praw


    【解决方案1】:

    可以使用get_comments 将参数limit 设置为None 来获取所有可用的cmets。 (默认情况下,它使用帐户的金额,通常为 25)。 (get_comments使用的参数包括get_content的参数,包括limit)。

    但是,这可能不会如您所愿——get_comments(或更具体地讲/r/subreddit/comments)仅提供新 cmets 或新镀金 cmets 列表,而不提供顶级 cmets。而且由于get_comments 的上限也为 1000 cmets,因此您将很难构建完整的顶级 cmets 列表。

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

    这里有一些代码:

    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
    

    【讨论】:

    • 看起来 get_subredditget_top 不再出现在当前版本的 praw 中。要使用最新版本运行,您需要删除单词 get: subreddit = r.subreddit('opensource') top = subreddit.top(params={'t': 'all'}, limit=25)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多