Question

I'm trying to scan a particular subreddit to see the how many times a comment appears in the top submissions.

I haven't been able to get any indication that it is actually reading the message, as it won't print the body of the message at all. Note: sr = subreddit phrase = phrase that's being looked for

I'm still new to praw and python (only picked it up in the last hour) but I've had a fair amount of experience in c.

Any help would be appreciated.

    submissions = r.get_subreddit(sr).get_top(limit=1)
    for submission in submissions:
        comments = praw.helpers.flatten_tree(submission.replace_more_comments(limit=None, threshold=0))
        for comment in comments:
            print(comment.body.lower())
            if comment.id not in already_done:
                if phrase in comment.body.lower():
                    phrase_counter = phrase_counter + 1
Était-ce utile?

La solution

The Submission.replace_more_comments return a list of the MoreComment objects that were NOT replaced. So if you're calling it with limit=None and threshold=0 then it will return an empty list. See the replace_more_comments docstring. Here's a full example of how to use both replace_more_comments and flatten_tree. For more information see the comment parsing page in our documentation.

import praw

r = praw.Reddit(UNIQUE_AND_DESCRIPTIVE_USERAGENT_CONTAINING_YOUR_REDDIT_USERNAME)
subreddit = r.get_subreddit('python')
submissions = subreddit.get_top(limit=1)
for submission in submissions:
    submission.replace_more_comments(limit=None, threshold=0)
    flat_comments = praw.helpers.flatten_tree(submission.comments)
    for comment in flat_comments:
        print(comment.body)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top