首页 > 解决方案 > 如何在 PRAW 中找到 Redditor 的名称?

问题描述

我如何从他发表的评论中找出 PRAW 中的 Redditor 的名字。例如,如果用户使用机器人的关键字评论某些内容,他们将收到 PM。要发送这个,必须获得 redditor 的名字。我尝试使用以下语法,但它不起作用并显示错误消息。

for comment in subreddit.stream.comments():
    if keyphrase in comment.body:
        plebman = comment.author
        reddit.redditor(plebman).message("TEST", "BOT TESTING")

出现此错误消息

plebman = comment.author
                      ^
TabError: inconsistent use of tabs and spaces in indentation

i我在 PRAW 中有什么功能可以这样做吗?

标签: pythonbotsredditpraw

解决方案


更新:我在 PRAW 文档中找到了这个命令,它可以从评论中获取作者的名字。以前的方法也很有效,但它被存储为一种名为“redditor”的未知数据类型。此方法将值存储为字符串,消除了我之前遇到的障碍。

然后

for comment in subreddit.stream.comments():
    if keyphrase in comment.body:
        plebman = comment.author
        reddit.redditor(plebman).message("TEST", "BOT TESTING")

现在(在第 3 行更改)

for comment in subreddit.stream.comments():
    if keyphrase in comment.body:
        plebman = comment.author.name
        reddit.redditor(plebman).message("TEST", "BOT TESTING")

或者总结一下

username = comment.author.name

这样,名称被存储为字符串,可以轻松使用。


推荐阅读