Get comments from a specific post

You can get the comments for a post/submission by creating/obtaining a Submission object and looping through the comments attribute. To get a post/submission we can either iterate through the submissions of a subreddit or specify a specific submission using reddit.submission and passing it the submission url or id.

To get the top-level comments we only need to iterate over submission.comments .

This will work for some submission, but for others that have more comments this code will throw an AttributeError saying:

AttributeError: 'MoreComments' object has no attribute 'body'

These MoreComments object represent the “load more comments” and “continue this thread” links encountered on the websites, as described in more detail in the comment documentation.

There get rid of the MoreComments objects, we can check the datatype of each comment before printing the body.

But Praw already provides a method called replace_more , which replaces or removes the MoreComments . The method takes an argument called limit, which when set to 0 will remove all MoreComments .

Both of the above code blocks successfully iterate over all the top-level comments and print their body. The output can be seen below.

Source: [https://www.facebook.com/VoyageursWolfProject/](https://www.facebook.com/VoyageursWolfProject/)

I thought this was a shit post made in paint before I read the title

Wow, that’s very cool. To think how keen their senses must be to recognize and avoid each other and their territories. Plus, I like to think that there’s one from the white colored clan who just goes way into the other territories because, well, he’s a badass.

That’s really cool. The edges are surprisingly defined.

...

However, the comment section can be arbitrarily deep and most of the time we surely also want to get the comments of the comments. CommentForest provides the .list method, which can be used for getting all comments inside the comment section.

The above code will first of output all the top-level comments, followed by the second-level comments and so on until there are no comments left.