2016-10-03 3 views
0

Не знаете, как это исправить? Пожалуйста, руководство:AttributeError: '<класс' 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 
Смежные вопросы