如何使用 API 获取 Reddit 提交的评论?

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

我可以使用此代码访问 subreddit

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/

如何获取特定提交的评论,例如第一个: https://www.reddit.com/r/AskReddit/comments/f08dxb/would_you_watch_a_show_where_a_billionaire_ceo/

PRAW 在 documentation that answers this question. See Comment Extraction and Parsing: Extracting comments with PRAW 中有一个部分。

根据链接的文档修改您的代码产量

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)

这将打印提交的所有顶级评论。请注意 Comment class 具有其他属性,其中许多已记录在案 here。例如,要打印用红色圈出的 comment 的一些属性,请尝试:

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

如链接文档所示,您可以使用 .list() 方法获取提交中的每条评论:

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)