我可以使用以下代码访问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在文档中有一个部分可以回答此问题。请参阅注释提取和解析:使用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)
谢谢。我已经在您之前的消息中找到了链接。有点毛茸茸的树逻辑结构。
是。它反映了Reddit上的界面。大多数评论都有一定数量的回复(可能为零),此外,可能有也可能没有“更多评论”按钮。如果要使用堆栈/队列或递归手动遍历树,则可以。但是
.list()
,如果您想要的话,该方法也有助于仅获取所有注释。