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

问题描述 投票:0回答:1

我可以使用此代码访问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/enter image description here

python reddit praw
1个回答
1
投票

PRAW在documentation中有一个部分回答了这个问题。参见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类具有其他属性,其中许多属性已记录在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)
© www.soinside.com 2019 - 2024. All rights reserved.