我正在使用reddit爬虫,并遇到以下错误。我是这个主题的新手,正在寻找克服错误的建议
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)
答案 0 :(得分:0)
在这种特定情况下,您可以通过将submission.author
强制转换为带有str(submission.author)
的字符串来解决此问题。 json
库无法序列化Redditor
对象(submission.author
是),但是可以序列化字符串。
因此,将func = [submission.title, submission.selftext, submission.author]
替换为func = [submission.title, submission.selftext, str(submission.author)]
。