It usually helps to add the stacktrace so people can see the actual error. However, as the PRAW maintainer, I know the error is something like MoreComments type has no attribute body
.
There are three simple ways to handle your problem. The first is to simply wrap the if "Cricketbot"
statement in a try/except and ignore the attribute error.
try:
if "Cricketbot..."
...
except AttributeError:
pass
That's not terribly exciting though. The second method is to ensure you're actually working with an object that has a body
attribute which can be done in two ways:
The first is to explicitly check if the attribute exists:
for comment in flat_comments:
if not hasattr(comment, 'body'):
continue
...
The second is to verify you are actually working with Comment
objects rather than the MoreComments
object:
for comment in flat_comments:
if not isinstance(comment, praw.objects.Comment):
continue
...
However, when running any of the above solutions, you are effectively not processing all the comments on a submission as you're missing anything that's hidden behind a MoreComments
object [ref]. To replace the MoreComments
object with some (replacing all can be very inefficient) of the comments requires usage of the replace_more_comments
function before flattening the tree:
submission = r.get_submission(submission_id=subid[i])
submission.replace_more_comments(limit=16, threshold=10)
flat_comments = praw.helpers.flatten_tree(submission.comments)
Setting limit=16
and threshold=10
means make no more than 16 additional requests, and only make requests that will result in at least 10 additional comments. You can play with those values as you wish, but note that each replacement requires an additional request (2 seconds) and some only yield as few as one comment.
I hope that helps.