Warm tip: This article is reproduced from stackoverflow.com, please click
praw python reddit

How to get a Reddit submission's comments using the API?

发布于 2020-04-23 11:04:32

I can access a subreddit with this code:

hot = praw.Reddit(...).subreddit("AskReddit").hot(limit=10)
for post in hot:
  print(post.title, post.url)

Would you watch a show where a billionaire CEO has to go an entire month on their lowest paid employees salary, without access to any other resources than that of the employee? What do you think would happen? https://www.reddit.com/r/AskReddit/comments/f08dxb/would_you_watch_a_show_where_a_billionaire_ceo/
All of the subreddits are invited to a house party. What kind of stuff goes down? https://www.reddit.com/r/AskReddit/comments/f04t6o/all_of_the_subreddits_are_invited_to_a_house/

How can I get the comments of a particular submission, for example the first one: https://www.reddit.com/r/AskReddit/comments/f08dxb/would_you_watch_a_show_where_a_billionaire_ceo/ enter image description here

Questioner
dereks
Viewed
14
jarhill0 2020-02-08 01:57

PRAW has a section in the documentation that answers this question. See Comment Extraction and Parsing: Extracting comments with PRAW.

Modifying your code based on the linked documentation yields

from praw.models import MoreComments

reddit = praw.Reddit(...)

hot = reddit.subreddit("AskReddit").hot(limit=10)
for submission in hot:
    print(submission.title)
    for top_level_comment in submission.comments:
        if isinstance(top_level_comment, MoreComments):
            continue
        print(top_level_comment.body)

This will print all of the top-level comments on the submission. Note that the Comment class has other attributes, many of which are documented here. For example, to print some of the attributes of a comment that you circled in red, try:

print(comment.author)
print(comment.score)
print(comment.created_utc)  # as a Unix timestamp
print(comment.body)

As the linked documentation suggests, you can get every comment in the submission using the .list() method:

reddit = praw.Reddit(...)

hot = reddit.subreddit("AskReddit").hot(limit=10)
for submission in hot:
    print(submission.title)
    submission.comments.replace_more(limit=None)
    for comment in submission.comments.list():
        print(comment.author)
        print(comment.score)
        print(comment.created_utc)  # as a Unix timestamp
        print(comment.body)