Guest User

Pulling All Comments from Reddit

a guest
Feb 28th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. import praw, json
  2.  
  3. #my set up. Everyone needs their own.
  4. #tutorial:
  5. #https://praw.readthedocs.io/en/latest/getting_started/authentication.html
  6. reddit = praw.Reddit(client_id='thiswontwork',
  7.                      client_secret='canttellya',
  8.                      password='redditpassword:token',
  9.                      user_agent='Give_it_a_name',
  10.                      username='your_reddit_user_name")
  11.  
  12.  
  13. #script to get all comments
  14. def getSubComments(comment, allComments, verbose=True):
  15.    allComments.append(comment)
  16.    if not hasattr(comment, "replies"):
  17.        replies = comment.comments()
  18.        if verbose: print("fetching (" + str(len(allComments)) + " comments fetched total)")
  19.    else:
  20.        replies = comment.replies
  21.    for child in replies:
  22.        getSubComments(child, allComments, verbose=verbose)
  23.  
  24.  
  25.  
  26. def getAll(r, submissionId, verbose=True):
  27.    submission = r.submission(submissionId)
  28.    comments = submission.comments
  29.    commentsList = []
  30.    for comment in comments:
  31.        getSubComments(comment, commentsList, verbose=verbose)
  32.    return commentsList
  33.  
  34.  
  35. #the second field is the thread ID you want to pull comments from.
  36. res = getAll(reddit, "au82b5")
  37.  
  38. #get rid of useless "MoreComments" elements in the list
  39. from praw.models import MoreComments
  40. comments = []
  41. for i in range(0,(len(res)-1)):
  42.    if isinstance(res[i], MoreComments):
  43.        continue
  44.    comments.append(res[i].body.encode('utf-8'))
  45.  
  46.  
  47. comm_json = []
  48. for comms in comments:
  49.    comm_json.append(json.dumps({"comments":comms}))
  50.  
  51. #and the comments are written in file "comment.json" in your current directory
  52. #it can be read by any text editor
  53. with open('comment.json', 'w') as outfile:
  54.    json.dump(comm_json, outfile)
Add Comment
Please, Sign In to add comment