2016-10-03 50 views
0

不知道如何解决这个问题?请指导:AttributeError:'<class'praw.objects.MoreComments'>'没有属性'score'

import praw 

r = praw.Reddit(user_agent="getting top posts from a subredit and its submissions", site_name='lamiastella') 
subreddit = r.get_subreddit('iama') 
top_year_subreddit = subreddit.get_top_from_year 
submissions = top_year_subreddit(limit=50) 
for submission in submissions: 
    #submission.replace_more_comments(limit=None, threshold=0) 
    all_comments = praw.helpers.flatten_tree(submission.comments) 
    all_comments.sort(key = lambda comment : comment.score, reverse = True) 
    top_comments = all_comments[:30] 
    for comment in top_comments: 
     print(comment.body) 
~        

的错误是:

$ python getSubmissionsFromSubreddit.py 
Traceback (most recent call last): 
    File "getSubmissionsFromSubreddit.py", line 10, in <module> 
    all_comments.sort(key = lambda comment : comment.score, reverse = True) 
    File "getSubmissionsFromSubreddit.py", line 10, in <lambda> 
    all_comments.sort(key = lambda comment : comment.score, reverse = True) 
    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 'score' 

这里的要点是:https://gist.github.com/monajalal/e3eb0590bc0a4f757500ec423f2b6dd3

我我的代码更新到这一点,并仍然得到错误检索顶部评论:

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)) 
       top_comments = all_comments.sort(key = lambda comment : comment.score, reverse = True) 
       for comment in top_comments[:10]: 
         print(comment.body) 
     else: 
       continue 

我收到此错误:

148 
Traceback (most recent call last): 
    File "getSubmissionsFromSubreddit.py", line 14, in <module> 
    for comment in top_comments[:10]: 
TypeError: 'NoneType' object has no attribute '__getitem__' 

回答

0

下面是正确答案:

import praw 

subreddit_name = 'nostalgia' 
num_submissions = 2 
num_top_comments = 10 
num_comments_per_submission = 500 
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_limit=num_comments_per_submission, 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)) 
       top_comments = sorted(all_comments, key=lambda comment: -comment.score) 
       for comment in top_comments[:10]: 
         print(comment.body) 
         if comment.author != None: 
           print('{0}: {1}'.format('Author name', comment.author.name)) 
         else: 
           print('Author account is DELETED') 
         print('{0}: {1}'.format('Comment score is', comment.score)) 
     else: 
       continue