I am trying to print all the comments from the top posts of a subreddit so that my bot can analyse them. I had it running earlier in the day, but I tried running it now and I have come across an error.
Here is my code:
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.
The starred section is where I am having the issue. I am trying to look through comments which have "Cricketbot, give me Australian news" written in them. Unfortunately, if the body of the comment isn't there, i.e. the comment is empty, the code returns an Attribute error and says that comment has no attribute 'body'.
How does one get around this issue?
AttributeError: '<class 'praw.objects.MoreComments'>' has no attribute 'body'
. I wrapped it in try and except and that worked, but the other one doesn't work (read, 'I don't understand how to use it'). It seems that it checks to see if the body exists, but then after the pass command, it just runs the code anyway. – Ahithophel