4

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 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

I dont see how it would be working in some instances but not in others.. in other words, why would a comment sometimes not have an author?
Why not try having a little play to see if you can pin down these cases. This is what I do if I get stuck. You can try a little for loop like for for comment in flat_comments: if comment.author is None: print comment Maybe this will be revealing
"why would a comment sometimes not have an author?" Perhaps because the author has deleted their account?
that was a great suggestion - turns out your line revealed a comment body of 'deleted' and author None. Thanks a ton!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.