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?
User Agent. A user agent is a unique identifier that helps Reddit determine the source of network requests. To use Reddit's API, you need a unique and descriptive user agent. The recommended format is <platform>:<app ID>:<version string> (by u/<Reddit username>) . For example, android:com.
If you're looking for how to reply to a comment, you want to use the Comment#reply method. If you have the comment bound to variable name c , then you can do the following: c. reply("Here is a reply to your comment.")
PRAW, an acronym for "Python Reddit API Wrapper", is a Python package that allows for simple access to Reddit's API. PRAW aims to be easy to use and internally follows all of Reddit's API rules. With PRAW there's no need to introduce sleep calls in your code. Give your client an appropriate user agent and you're set.
As its name suggests, PRAW is a Python wrapper for the Reddit API, which enables you to scrape data from subreddits, create a bot, and much more. In this article, we will learn how to use PRAW to scrape posts from different subreddits and get comments from a specific post.
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
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With