2016-10-03 1 views
1

Comment réparer cette erreur?AttributeError: '<classe' praw.objects.MoreComments '>' n'a aucun attribut 'body'

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 

-je obtenir:

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' 

Répondre

0

erreur stupide, mais aurait dû submission.replace_more_comments(limit=None, threshold=0) au-dessus all_comments = praw.helpers.flatten_tree(submission.comments) ligne:

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