Python: TypeError: argument of type 'Comment' is not iterable

user3750888 picture user3750888 · Jun 18, 2014 · Viewed 11.9k times · Source

I'm trying to code a simple reddit bot which will get into a subreddit, get into a submission, read the comments, and if a comment says 'feels' it'll post a feels gif. I get this error: 'TypeError: argument of type 'Comment' is not iterable' when trying to do has_feels = 'feels' in comment.

My code:

import praw
import time

r = praw.Reddit('Posts Feels gif in response to someone saying feels'
                'by: Mjone77')
r.login('Feels_Bot', 'notrealpassword')
already_done = []

feels = ['feels']
while True:
    subreddit = r.get_subreddit('bottest')
    for submission in subreddit.get_new(limit=10):
        #submission = next(submissions)
        commentNum = 0
        for comment in submission.comments:
            print(comment)
            print(comment.id)
            has_feels = 'feels' in comment
            if comment.id not in already_done and has_feels:
                #comment.reply('[Relevant](http://i.imgur.com/pXBrf.gif)')
                already_done.append(comment.id)
                print('Commented')
        time.sleep(1800)

Error report (first two lines are the print outs of the code until it breaks):

The feels are strong
ciafpqn
Traceback (most recent call last):
  File "C:\Users\Me\Desktop\My Programs\Feels Bot\Feels Bot\FeelsBot.py", line 18, in <module>
    has_feels = 'feels' in comment
TypeError: argument of type 'Comment' is not iterable
sys:1: ResourceWarning: unclosed <socket object at 0x0369D8E8>
C:\Python34\lib\importlib\_bootstrap.py:2150: ImportWarning: sys.meta_path is empty

Anyone know how to fix this so if comment contains 'feels' somewhere inside of it it will set has_feels to true?

Also once it does go through all of the comments in a submission, it just stops it doesn't go onto the next submission. Anyone know how to fix that? If you don't know right away don't bother looking anything up for me I can find that part out.

Answer

Martin Konecny picture Martin Konecny · Jun 18, 2014
has_feels = 'feels' in comment

comment here looks to be an object with attributes which inherently is not iterable like a list or a string.

From the praw documentation, you need to access the comment's text via the body attribute:

So something like:

has_feels = 'feels' in comment.body

Here's some examples about why what you're current doing does not work:

>>> "x" in "xxyy"  # works (string is iterable)
>>> "x" in ["x", "y"]  # works (list is iterable)

>>> class MyClass(): pass
>>> c = MyClass()
>>> "y" in c
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'instance' is not iterable