Search code examples
pythonredditpraw

Loop through all comments for a substring


I'm writing a program that would go over a list of strings containing artists' names and compare it to all the comments on a Reddit submission. It stops after finding one match or doesn't work at all(even with simple test strings), can you point me to the bug? Including the Reddit parts excluding authentication.

submission = reddit.submission(id='75lnoo') # Topic about Eminem, lots of mentions of him
submission.comments.replace_more(limit=0)  # Stores all the comments
comments = submission.comments.list()
artists_list = ['Eminem', 'Drake'] # Sample list
for comment in comments:
    for artist in artists_list:
        if artist.lower() in comment.body.lower():
            print(comment.permalink() + ' - ' + artist)

Would only print one thing when there should be plenty of matches

/r/Music/comments/75lnoo/eminem_rips_donald_trump_in_bet_hip_hop_awards/do894hp - Eminem


Solution

  • Just ran the code locally on my machine I got a lot of results for Eminem and none for Drake. My guess since this threw me off at first was it took a while for it to get the second results after the first. It could be that you are terminated the program early thinking all results are printed?

    Here a direct copy-paste:

     import praw
    
     reddit = praw.Reddit(client_id = '',
                     client_secret= '',
                     user_agent= '',
                     username = '',
                     password = '')
    
    submission = reddit.submission(id='75lnoo')
    submission.comments.replace_more(limit=0)  # Stores all the comments
    comments = submission.comments.list()
    artists_list = ['Eminem', 'Drake'] # Sample list
    print(artists_list)
    for comment in comments:
    for artist in artists_list:
        if artist.lower() in comment.body.lower():
            print(comment.permalink() + ' - ' + artist)