我正在尝试打印 subreddit 顶部帖子中的所有评论,以便我的机器人可以分析它们。我在当天早些时候运行它,但我现在尝试运行它,但遇到了一个错误。
这是我的代码:
r = praw.Reddit('Comment crawler v1.0 by /u/...')
r.login('username', 'password')
subreddit=r.get_subreddit('subreddit')
post_limit = 25
subreddit_posts = subreddit.get_hot(limit=post_limit)
subids = set()
for submission in subreddit_posts:
subids.add(submission.id)
subid = list(subids)
i=0
while i < post_limit:
submission = r.get_submission(submission_id=subid[i])
flat_comments = praw.helpers.flatten_tree(submission.comments)
with open('alreadydone.txt', 'r') as f:
already_done = [line.strip() for line in f]
f.close()
for comment in flat_comments:
if "Cricketbot, give me Australian news" in **comment.body** and comment.id not in already_done:
info = feedparser.parse(Australia) #Australia gives a link to an RSS feed.
加星标的部分是我遇到问题的地方。我正在尝试查看写有“Cricketbot,给我澳大利亚新闻”的评论。不幸的是,如果注释的正文不存在,即注释为空,代码将返回一个属性错误并说注释没有属性“正文”。
如何解决这个问题?