【发布时间】:2020-05-14 03:14:40
【问题描述】:
我正在使用 praw 和 Python 3 从 subreddits 列表中抓取帖子和 cmets。该代码以前适用于 1 个 subreddit 以及 [i] 个 subreddit 列表中的 [j] 个搜索词列表。我删除了搜索词列表,只是希望它遍历 subreddit 列表,但我不断收到“TypeError:'Subreddit' object is not iterable。我不明白发生了什么?
subs= ["sub1","sub2", "sub3", "sub4"]
commentsDict = {"comment_user": [], "comment_text":[], "comment_score":[], "comment_date":[] }
postsDict = {"post_title" : [], "post_score" : [], "post_comments_num":[], "post_date":[], \
"post_user":[], "post_text":[], "post_id":[]}
for i in range(len(subs)):
for submission in reddit.subreddit(subs[i]):
submission.comment_sort = 'new'
comments = list(submission.comments)
for comments in submission.comments:
postsDict["post_title"].append(submission.title)#title of post with comment
postsDict["post_score"].append(submission.score)#upvotes-downvotes
postsDict["post_text"].append(submission.selftext)#get body of post
postsDict["post_id"].append(submission.id)#unique id address for post
postsDict["post_user"].append(submission.author) #user name of poster
postsDict["post_comments_num"].append(submission.num_comments) #number of comments on post
date = submission.created_utc #create variable for date
timestamp = datetime.datetime.fromtimestamp(date) #create variable to translate unix date
postsDict["post_date"].append(timestamp.strftime('%Y-%m-%D %H:%M:%S')) #extract date and add to dict
for top_level_comment in submission.comments: #create loop for extracting comments
if isinstance(top_level_comment, MoreComments):
continue
submission.comments.replace_more(limit=None) #tell Praw to click more comments and get those too
commentsDict["comment_user"].append(comments.author) #get comment username
commentsDict["comment_score"].append(comments.score) #comment upvotes-downvotes
date = comments.created #same date as above but for comments
timestamp = datetime.datetime.fromtimestamp(date)
commentsDict["comment_date"].append(timestamp.strftime('%Y-%m-%D %H:%M:%S')) #add translated unix date to dict
commentsDict["comment_text"].append(comments.body) #get comment text
提前感谢您的帮助。
【问题讨论】:
标签: python-3.x praw