如何解决此错误?
import praw
subreddit_name = 'relationships'
num_submissions = 30
r = praw.Reddit(user_agent="getting top posts from a subredit and its submissions", site_name='lamiastella')
subreddit = r.get_subreddit(subreddit_name)
top_submissions = subreddit.get_top_from_year(limit = num_submissions)
for submission in top_submissions:
all_comments = praw.helpers.flatten_tree(submission.comments)
submission.replace_more_comments(limit=None, threshold=0)
if len(all_comments) > 100:
print(len(all_comments))
#top_comments = all_comments.sort(key = lambda comment : comment.score, reverse = True)
for comment in all_comments[:100]:
print(comment.body)
else:
continue
我明白了:
Your mother would be very proud of you, you did an awesome job. Good luck with you and your father's therapy.
[removed]
[removed]
Traceback (most recent call last):
File "getSubmissionsFromSubreddit.py", line 16, in <module>
print(comment.body)
File "/usr/local/lib/python2.7/dist-packages/praw/objects.py", line 92, in __getattr__
raise AttributeError(msg)
AttributeError: '<class 'praw.objects.MoreComments'>' has no attribute 'body'
答案 0 :(得分:2)
愚蠢的错误,但应该submission.replace_more_comments(limit=None, threshold=0)
以上all_comments = praw.helpers.flatten_tree(submission.comments)
行:
import praw
subreddit_name = 'nostalgia'
num_submissions = 2
r = praw.Reddit(user_agent="getting top posts from a subredit and its submissions", site_name='lamiastella')
subreddit = r.get_subreddit(subreddit_name)
top_submissions = subreddit.get_top_from_year(limit = num_submissions, comment_sort='top')
for submission in top_submissions:
submission.replace_more_comments(limit=None, threshold=0)
all_comments = praw.helpers.flatten_tree(submission.comments)
if len(all_comments) > 100:
print(len(all_comments) all_comments = praw.helpers.flatten_tree(submission.comments))
#top_comments = all_comments.sort(key = lambda comment : comment.score, reverse = True)
for comment in all_comments[:10]:
print(comment.body)
else:
continue
答案 1 :(得分:2)
好像新版本的praw一样,将replace_more_comments()
函数从Submission
类移到了Comment
类。修改评论树的修改方法如下:
submission.comments.replace_more(limit=0) # flatten tree
comments = submission.comments.list() # all comments
更多信息,请访问:https://praw.readthedocs.io/en/latest/code_overview/models/submission.html