PRAW: Comment Submitter's Username

倾然丶 夕夏残阳落幕 提交于 2019-12-06 03:01:12

问题


I'm developing a reddit bot that needs to know which user submitted a comment.

According to the PRAW API wrapper docs, there's no specific way to get the username of a Comment object's author. Ideally I could directly get the username back. If that's not possible, is there a way to get the fullname of the author and then convert it to a username?


回答1:


I'm a maintainer for PRAW. Where does it say that you cannot get the username of a Comment objects author? Because that is incorrect and needs to be fixed.

Anyway, a Comment has a author attribute, which is a Redditor instance of the author.

import praw

r = praw.Reddit(UNIQUE_AND_DESCRIPTIVE_USERAGENT)
submission = r.get_submission("http://www.reddit.com/r/redditdev/comments/16m0uu/praw_20_is_coming_release_in_2_days/")
comment = submission.comments[0]
author = comment.author  # This returns a ``Redditor`` object.
print(author.name)  # The username



回答2:


Can't comment as don't have enough reputation. @Humus mentioned in his comment that it was no where mentioned in the PRAW readthedocs.org documentation. There is a simple workaround.We can use dir(object_name) to get a list of the attributes for that object. Then it is just a guessing game thereafter.

Edit: You can also use pprint(vars(object_name))



来源:https://stackoverflow.com/questions/20822808/praw-comment-submitters-username

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!