Created
April 30, 2019 03:55
-
-
Save jarhill0/6e6495706252d52573950c3820f533b0 to your computer and use it in GitHub Desktop.
Sample code for converting a PRAW Comment to JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import praw | |
from json import dumps, JSONEncoder | |
reddit = praw.Reddit(username='', | |
password='', | |
client_id='', | |
client_secret='', | |
user_agent='') | |
comment = reddit.comment('em3rygg') | |
print(comment.body) # to fetch the comment | |
class PRAWJSONEncoder(JSONEncoder): | |
"""Class to encode PRAW objects to JSON.""" | |
def default(self, obj): | |
if isinstance(obj, praw.models.base.PRAWBase): | |
obj_dict = {} | |
for key, value in obj.__dict__.items(): | |
if not key.startswith('_'): | |
obj_dict[key] = value | |
return obj_dict | |
else: | |
return JSONEncoder.default(self, obj) | |
print(dumps(comment, cls=PRAWJSONEncoder)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment