1

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
ZeroPanda
  • 133
  • 1
  • 9
  • 1
    Can you be more specific with what you mean by "it seems it does not provide the full output." Please provide example output and also an example of what you would expect. – jarhill0 Jun 27 '20 at 01:53
  • 1
    It only shows the first comment, and I want all the comments in the thread that include the word "trump". Sorry for not being specific. – ZeroPanda Jun 27 '20 at 01:56

1 Answers1

3

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)
lffloyd
  • 313
  • 2
  • 8
  • Thanks a lot. Does the hot page have stream function? Like, is subreddit = reddit.subreddit('worldnews').hot() possible? – ZeroPanda Jun 27 '20 at 03:41
  • 1
    Only using "Trump" as a search term might reduce the possible results you can get... also add other terms in a list like ['Trump','Donald','trump','donald'] etc and check if words in this list exist in the comments... – Sabito stands with Ukraine Jun 27 '20 at 03:46
  • @user9909958 [this link](https://stackoverflow.com/questions/50500360/can-you-stream-posts-that-have-made-it-to-hot) has a good explanation for what is the purpose of the stream, and why it doesn't really meshes well with the "hot" posts concept. To put it shortly, what you want with stream is to get the comments/replies/submissions when they are created, while the concept behind hot posts is that you want the content that is currently making more "success". So, for a post make success, it will probably need some time to gain replies/likes – lffloyd Jun 29 '20 at 23:02