温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - How to get a Reddit submission's comments using the API?
praw python reddit

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

发布于 2020-04-25 10:21:50

我可以使用以下代码访问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/ 在此处输入图片说明

查看更多

提问者
dereks
被浏览
14
jarhill0 2020-02-08 01:57

PRAW在文档有一个部分可以回答此问题。请参阅注释提取和解析:使用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该类还有其他属性,此处记录许多属性例如,要打印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)