如何使用 API 获取 Reddit 提交的评论? [英] How to get a Reddit submission's comments using the API?

查看:47
本文介绍了如何使用 API 获取 Reddit 提交的评论?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用此代码访问

解决方案

PRAW 在 文档 中有一节这回答了这个问题.请参阅评论提取和解析:提取对 PRAW 的评论.

根据链接的文档修改您的代码收益

from praw.models import MoreCommentsreddit = praw.Reddit(...)hot = reddit.subreddit("AskReddit").hot(limit=10)热提交:打印(提交.标题)对于 submit.comments 中的 top_level_comment:if isinstance(top_level_comment, MoreComments):继续打印(top_level_comment.body)

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

print(comment.author)打印(评论.分数)print(comment.created_utc) # 作为 Unix 时间戳打印(评论.正文)

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

reddit = praw.Reddit(...)hot = reddit.subreddit("AskReddit").hot(limit=10)热提交:打印(提交.标题)submit.comments.replace_more(limit=None)对于 submit.comments.list() 中的评论:打印(评论.作者)打印(评论.分数)print(comment.created_utc) # 作为 Unix 时间戳打印(评论.正文)

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/

解决方案

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)

这篇关于如何使用 API 获取 Reddit 提交的评论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆