简体   繁体   中英

TypeError: Object of type 'Redditor' is not JSON serializable

I'm working on reddit crawler and got the following error. I'm new to this subject and looking for suggestions to overcome the error

import praw
import json

reddit = praw.Reddit(client_id= client_id, \
                     client_secret= client_secret, \
                     user_agent= user1, \
                     username= username, \
                     password= password)

def prawSubreddit(subName, lm):
    print("Collecting from /r/{}...".format(subName))
    subreddit = reddit.subreddit(subName)
    submissions = subreddit.top(limit=lm)
    redditData = []

    for submission in submissions:
        keys = ['Title', 'Txt', 'Author']
        func = [submission.title, submission.selftext, submission.author]
        redditData.append(dict(zip(keys,func)))

    print("Finished Collecting.")
    writeOutput("{}.txt".format(subName),redditData)
def writeOutput(fileName, data):
   `outputFile = open(fileName, "w")
    outputFile.write(json.dumps(data, sort_keys = True))

if __name__ == '__main__':
  prawSubreddit('opiates', 5)

In this specific instance, you can fix the problem by casting submission.author to a string with str(submission.author) . The json library can't serialize a Redditor object (which is what submission.author is), but it can serialize a string.

So, replace func = [submission.title, submission.selftext, submission.author] with func = [submission.title, submission.selftext, str(submission.author)] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM