不确定如何解决这个问题?请指导:
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 :(得分: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