AttributeError:' '没有属性'身体'

问题描述 投票:2回答:2

我该如何解决这个错误?

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'
python attributeerror praw reddit
2个回答
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

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

© www.soinside.com 2019 - 2024. All rights reserved.