ZeroPanda
ZeroPanda

Reputation: 123

Scraping stream of subreddits, comments, and replies from reddit api (praw) in python

I want to print the live feed of trump from Reddit in python. The output involves any thread, comment, or reply that includes "trump" in it. I am trying this code but it seems it does not provide the full output.

import praw

reddit = praw.Reddit(client_id='.....',
                     client_secret='.....', password='....',
                     user_agent='testscript by /u/......', username='.....')

subreddit = reddit.subreddit('worldnews')

findme = "Trump"

for comment in subreddit.stream.comments():
    try:
        parent_id = str(comment.parent())
        submission = reddit.comment(parent_id)

        if submission.body.find(findme) != -1:
            print(submission.body)
            print('\n')
            if comment.body.find(findme) != -1:
                print(comment.body)
                for reply in submission.replies:
                    print(reply)
        else:
            continue
    except praw.exceptions.PRAWException as e:
        pass

Upvotes: 1

Views: 2441

Answers (1)

lffloyd
lffloyd

Reputation: 333

As you are working with a stream, you wouldn't probably get all the comments on the submission that include the given word. Comments appear as they become available, and at that moment, they probably doesn't have any replies. Also, older comments - written before your script began - with the given keyword will not be catch by the stream.

Additionally, the only problem in your code is that you don't check if the replies really have "Trump" on their bodies:

for reply in submission.replies:
    if reply.body.find(findme) != -1:
        print(reply)

Upvotes: 3

Related Questions