r/redditdev•
CJDrew

[PRAW] Problems Using Base Reddit Class

PRAW

As an entry into using PRAW, I'm attempting to write a bot that will scan all the comments of a submission and tally up the total number of uses of each word in the thread.

This is the code I have written so far

    countList = []
    wordList = []
    
    reddit = praw.Reddit(client_id=_client_id, client_secret=_client_secret, user_agent=_user_agent)
    submission = reddit.get_submission(submission_id='6pkw0q')
    for comment in submission.comments.list():
        words = comment.body.split()
        for word in words:
            if not word in wordList:
                wordList.append(word)
                countList.append(0)
            else:
                x = 0
                for existingWord in wordList:
                    if existingWord == word:
                        countList[x] += 1
                    else:
                        x+=1

The error I get when I try to run is this:

AttributeError: 'Reddit' object has no attribute 'get_submission'

I don't know whether the rest of the code works since it won't run past the line that throws the error.

Any help would be appreciated

Posted Jul 26, 2017, 5:25 AM UTC

25
GoldenSights•

It looks like that came from an earlier version of PRAW, the new way is reddit.submission('6pkw0q'). A lot of the verby names have been changed, so if you see any r.get_ it's probably old. It's usually pretty similar though, and if you can't find it in the docs it's always fine to ask here.

By the way, the better data structure to use for your counter is a dictionary, where you can relate words to numbers without having to fiddle with indices like that. Take a look at these:

https://docs.python.org/3/tutorial/datastructures.html#dictionaries

https://stackoverflow.com/questions/3496518/python-using-a-dictionary-to-count-the-items-in-a-list

3
CJDrewOP•

Thank you so much! I'll look into it. Is there somewhere where I can look up functions in the future? Whatever I was looking at was obviously outdated

2
GoldenSights•

http://praw.readthedocs.io/en/latest/ is the official docs page. Check out the code overview and the pages for the classes you're interested in.

I would also recommend opening up the Python interpreter in your command line and playing with the dir function, which shows a list of attributes and methods to try:

>>> dir(reddit)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__'
, '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__',
 '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__we
akref__', '_authorized_core', '_check_for_update', '_core', '_next_unique', '_objector', '_prepare_objector', '_prepare_
prawcore', '_prepare_trusted_prawcore', '_prepare_untrusted_prawcore', '_read_only_core', '_unique_counter', 'auth', 'co
mment', 'config', 'domain', 'front', 'get', 'inbox', 'info', 'live', 'multireddit', 'post', 'random_subreddit', 'read_on
ly', 'redditor', 'request', 'submission', 'subreddit', 'subreddits', 'update_checked', 'user']
>>>
>>> reddit.subreddit
<praw.models.helpers.SubredditHelper object at 0x03FB96B0>
>>> reddit.subreddit('redditdev')
Subreddit(display_name='redditdev')
2
CJDrewOP•

Perfect. Thank you

2
doug89Bot Developer•

Don't forget about help().

>>> c = reddit.comment('dkqdvl9')
>>> help(c)
Help on Comment in module praw.models.reddit.comment object:

class Comment(praw.models.reddit.base.RedditBase, praw.models.reddit.mixins.inboxable.InboxableMixin, praw.models.reddit.mixins.UserContentMixin)
 |  A class that represents a reddit comments.
 |
 |  Method resolution order:
 |      Comment
 |      praw.models.reddit.base.RedditBase
 |      praw.models.base.PRAWBase
 |      praw.models.reddit.mixins.inboxable.InboxableMixin
 |      praw.models.reddit.mixins.UserContentMixin
 |      praw.models.reddit.mixins.editable.EditableMixin
 |      praw.models.reddit.mixins.gildable.GildableMixin
 |      praw.models.reddit.mixins.inboxtoggleable.InboxToggleableMixin
 |      praw.models.reddit.mixins.replyable.ReplyableMixin
 |      praw.models.reddit.mixins.reportable.ReportableMixin
 |      praw.models.reddit.mixins.savable.SavableMixin
 |      praw.models.reddit.mixins.votable.VotableMixin
 |      builtins.object
 |
 |  Methods defined here:
 |
 |  __init__(self, reddit, id=None, _data=None)
 |      Construct an instance of the Comment object.
 |
 |  __setattr__(self, attribute, value)
 |      Objectify author, replies, and subreddit.
 |
 |  parent(self)
 |      Return the parent of the comment.
 |
 |      The returned parent will be an instance of either
 |      :class:`.Comment`, or :class:`.Submission`.
 |
 |      If this comment was obtained through a :class:`.Submission`, then its
 |      entire ancestry should be immediately available, requiring no extra
 |      network requests. However, if this comment was obtained through other
 |      means, e.g., ``reddit.comment('COMMENT_ID')``, or
 |      ``reddit.inbox.comment_replies``, then the returned parent may be a
 |      lazy instance of either :class:`.Comment`, or :class:`.Submission`.
 |
 |      Lazy Comment Example:
 |
 |      .. code:: python
 |
 |         comment = reddit.comment('cklhv0f')
 |         parent = comment.parent()
 |         # `replies` is empty until the comment is refreshed
 |         print(parent.replies)  # Output: []
 |         parent.refresh()
 |         print(parent.replies)  # Output is at least: [Comment(id='cklhv0f')]
 |
 |  permalink(self, fast=False)
 |      Return a permalink to the comment.
 |
 |      :param fast: Return the result as quickly as possible (default: False).
 |
 |      In order to determine the full permalink for a comment, the Submission
 |      may need to be fetched if it hasn't been already. Set ``fast=True`` if
 |      you want to bypass that possible load.
 |
 |      A full permalink looks like:
 |      [r/redditdev](/r/redditdev)/comments/2gmzqe/praw_https_enabled/cklhv0f
 |
 |      A fast-loaded permalink for the same comment will look like:
 |      /comments/2gmzqe//cklhv0f
 |
 |  refresh(self)
 |      Refresh the comment's attributes.
 |
 |      If using :meth:`.Reddit.comment` this method must be called in order to
 |      obtain the comment's replies.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  is_root
 |      Return True when the comment is a top level comment.
 |
 |  mod
 |      Provide an instance of :class:`.CommentModeration`.
 |
 |  replies
 |      Provide an instance of :class:`.CommentForest`.
 |
 |  submission
 |      Return the Submission object this comment belongs to.
 |
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |
 |  STR_FIELD = 'id'
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from praw.models.reddit.base.RedditBase:
 |
 |  __eq__(self, other)
 |      Return whether the other instance equals the current.
 |
 |  __getattr__(self, attribute)
 |      Return the value of `attribute`.
 |
 |  __hash__(self)
 |      Return the hash of the current instance.
 |
 |  __ne__(self, other)
 |      Return whether the other instance differs from the current.
 |
 |  __repr__(self)
 |      Return an object initialization representation of the instance.
 |
 |  __str__(self)
 |      Return a string representation of the instance.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from praw.models.reddit.base.RedditBase:
 |
 |  fullname
 |      Return the object's fullname.
 |
 |      A fullname is an object's kind mapping like ``t3`` followed by an
 |      underscore and the object's base36 ID, e.g., ``t1_c5s96e0``.
 |
 |  ----------------------------------------------------------------------
 |  Class methods inherited from praw.models.base.PRAWBase:
 |
 |  parse(data, reddit) from builtins.type
 |      Return an instance of ``cls`` from ``data``.
 |
 |      :param data: The structured data.
 |      :param reddit: An instance of :class:`.Reddit`.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from praw.models.base.PRAWBase:
 |
 |  __dict__
 |      dictionary for instance variables (if defined)
 |
 |  __weakref__
 |      list of weak references to the object (if defined)
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from praw.models.reddit.mixins.inboxable.InboxableMixin:
 |
 |  block(self)
 |      Block the user who sent the item.
 |
 |      .. note:: Reddit does not permit blocking users unless you have a
 |                :class:`.Comment` or :class:`.Message` from them in your
 |                inbox.
 |
 |  mark_read(self)
 |      Mark the item as read.
 |
 |      .. note:: This method pertains only to objects which were retrieved via
 |                the inbox.
 |
 |  mark_unread(self)
 |      Mark the item as unread.
 |
 |      .. note:: This method pertains only to objects which were retrieved via
 |                the inbox.
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from praw.models.reddit.mixins.editable.EditableMixin:
 |
 |  delete(self)
 |      Delete the object.
 |
 |  edit(self, body)
 |      Replace the body of the object with ``body``.
 |
 |      :param body: The markdown formatted content for the updated object.
 |      :returns: The current instance after updating its attributes.
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from praw.models.reddit.mixins.gildable.GildableMixin:
 |
 |  gild(self)
 |      Gild the author of the item.
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from praw.models.reddit.mixins.inboxtoggleable.InboxToggleableMixin:
 |
 |  disable_inbox_replies(self)
 |      Disable inbox replies for the item.
 |
 |  enable_inbox_replies(self)
 |      Enable inbox replies for the item.
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from praw.models.reddit.mixins.replyable.ReplyableMixin:
 |
 |  reply(self, body)
 |      Reply to the object.
 |
 |      :param body: The markdown formatted content for a comment.
 |      :returns: A :class:`~.Comment` object for the newly created comment.
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from praw.models.reddit.mixins.reportable.ReportableMixin:
 |
 |  report(self, reason)
 |      Report this object to the moderators of its subreddit.
 |
 |      :param reason: The reason for reporting.
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from praw.models.reddit.mixins.savable.SavableMixin:
 |
 |  save(self, category=None)
 |      Save the object.
 |
 |      :param category: (Gold) The category to save to. If your user does not
 |          have gold this value is ignored by Reddit (default: None).
 |
 |  unsave(self)
 |      Unsave the object.
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from praw.models.reddit.mixins.votable.VotableMixin:

-snip- too long.

2