Praw AttributeError: 'NoneType' object has no attribute 'get_comments'

1.7k Views Asked by At

I wrote a simple script for identifying users who contribute to certain subreddits. As a disclaimer, if you plan on using this code you should be sure to anonymize the data (as I will, by aggregating data and removing all usernames). It works with certain subreddits but does not seem to be very robust, as seen by the following error I get when I run it with /r/nba:

AttributeError: 'NoneType' object has no attribute 'get_comments'

Below is my code:

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)

When I change the subreddit to 'python' it seems to encounter less problems, so hopefully someone can point out what I'm missing. Thanks in advance!

1

There are 1 best solutions below

4
On BEST ANSWER

Ok, so you see the line

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

I presume your code is failing because

comment.author is None