gpt4 book ai didi

python - 使用 praw 解析评论时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-03 18:32:17 26 4
gpt4 key购买 nike

我正在尝试扫描特定的 Reddit 子版 block ,以查看评论在热门提交中出现的次数。

我无法得到任何迹象表明它实际上正在读取消息,因为它根本不会打印消息正文。注意:sr = Reddit 子版 block 短语 = 正在查找的短语

我对 praw 和 python 还很陌生(最近一小时才学会),但我在 c 方面有相当多的经验。

如有任何帮助,我们将不胜感激。

    submissions = r.get_subreddit(sr).get_top(limit=1)
for submission in submissions:
comments = praw.helpers.flatten_tree(submission.replace_more_comments(limit=None, threshold=0))
for comment in comments:
print(comment.body.lower())
if comment.id not in already_done:
if phrase in comment.body.lower():
phrase_counter = phrase_counter + 1

最佳答案

Submission.replace_more_comments 返回替换的 MoreComment 对象列表。因此,如果您使用 limit=Nonethreshold=0 调用它,那么它将返回一个空列表。请参阅replace_more_comments文档字符串。以下是如何使用 replace_more_commentsflatten_tree 的完整示例。欲了解更多信息,请参阅comment parsing我们文档中的页面。

import praw

r = praw.Reddit(UNIQUE_AND_DESCRIPTIVE_USERAGENT_CONTAINING_YOUR_REDDIT_USERNAME)
subreddit = r.get_subreddit('python')
submissions = subreddit.get_top(limit=1)
for submission in submissions:
submission.replace_more_comments(limit=None, threshold=0)
flat_comments = praw.helpers.flatten_tree(submission.comments)
for comment in flat_comments:
print(comment.body)

关于python - 使用 praw 解析评论时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22211720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com