Question

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?

Était-ce utile?

La solution

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

Autres conseils

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))

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top