Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import praw, json
- #my set up. Everyone needs their own.
- #tutorial:
- #https://praw.readthedocs.io/en/latest/getting_started/authentication.html
- reddit = praw.Reddit(client_id='thiswontwork',
- client_secret='canttellya',
- password='redditpassword:token',
- user_agent='Give_it_a_name',
- username='your_reddit_user_name")
- #script to get all comments
- def getSubComments(comment, allComments, verbose=True):
- allComments.append(comment)
- if not hasattr(comment, "replies"):
- replies = comment.comments()
- if verbose: print("fetching (" + str(len(allComments)) + " comments fetched total)")
- else:
- replies = comment.replies
- for child in replies:
- getSubComments(child, allComments, verbose=verbose)
- def getAll(r, submissionId, verbose=True):
- submission = r.submission(submissionId)
- comments = submission.comments
- commentsList = []
- for comment in comments:
- getSubComments(comment, commentsList, verbose=verbose)
- return commentsList
- #the second field is the thread ID you want to pull comments from.
- res = getAll(reddit, "au82b5")
- #get rid of useless "MoreComments" elements in the list
- from praw.models import MoreComments
- comments = []
- for i in range(0,(len(res)-1)):
- if isinstance(res[i], MoreComments):
- continue
- comments.append(res[i].body.encode('utf-8'))
- comm_json = []
- for comms in comments:
- comm_json.append(json.dumps({"comments":comms}))
- #and the comments are written in file "comment.json" in your current directory
- #it can be read by any text editor
- with open('comment.json', 'w') as outfile:
- json.dump(comm_json, outfile)
Add Comment
Please, Sign In to add comment