【问题标题】:Praw AttributeError: 'NoneType' object has no attribute 'get_comments'Praw AttributeError:“NoneType”对象没有属性“get_comments”
【发布时间】:2015-01-07 06:29:18
【问题描述】:

我编写了一个简单的脚本来识别为某些子版块做出贡献的用户。作为免责声明,如果您打算使用此代码,则应确保对数据进行匿名化(我将通过聚合数据并删除所有用户名)。它适用于某些 subreddits,但似乎不是很健壮,正如我使用 /r/nba 运行它时出现的以下错误所示:

AttributeError: 'NoneType' 对象没有属性 'get_cmets'

下面是我的代码:

import praw
import pprint
users = [] #[username, flair, comments]

r=praw.Reddit(user_agent="user_agent")
r.login("username", "password")
submissions = r.get_subreddit('nba').get_top(limit=1) #won't work with higher limit?
for submission in submissions:
    submission.replace_more_comments(limit=3, threshold=5)
    flat_comments = praw.helpers.flatten_tree(submission.comments)
    for comment in flat_comments:
        user_comments = []
        for i in comment.author.get_comments(limit=2):
            user_comments.append(i.body)
            #user_comments.append(str(i.body)) #sometimes causes an error as well
        users.append([str(comment.author), comment.author_flair_text, user_comments])

pprint.pprint(users)

当我将 subreddit 更改为“python”时,它似乎遇到的问题更少,所以希望有人能指出我所缺少的。提前致谢!

【问题讨论】:

    标签: python reddit praw


    【解决方案1】:

    好的,所以你看到了这条线

    for i in comment.author.get_comments(limit=2):
    

    我认为您的代码失败是因为

    comment.author is None
    

    【讨论】:

    • 我看不出它在某些情况下会如何工作,但在其他情况下却不行。换句话说,为什么评论有时会没有作者?
    • 为什么不试试看你能不能确定这些案例。如果我被卡住了,这就是我要做的。您可以尝试一些 for 循环,例如 for comment in flat_cmets:如果 comment.author 为 None:打印评论也许这会揭示
    • "为什么评论有时没有作者?"也许是因为作者删除了他们的帐户?
    • 这是一个很好的建议 - 结果你的行显示了一个“已删除”的评论正文和作者无。非常感谢!
    猜你喜欢
    • 2019-01-01
    • 2021-12-26
    • 2019-07-23
    • 2018-05-13
    • 2020-09-07
    • 2017-05-03
    • 2023-03-16
    • 2018-07-14
    • 2013-06-16
    相关资源
    最近更新 更多