问题
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