Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Reddit user comments using PRAW causing TypeError: 'SubListing' object is not callable error

Tags:

python

praw

I'm trying to retrieve the last 1000 comments from a user since 1000 is the Reddit limit.

I followed the code example here, and modified a few of the calls for the updated API. Such as user.get_comments now seems to be just user.comments.

Here is the code I've run.

import praw

my_user_agent = 'USERAGENT'
my_client_id = 'CLIENTID'
my_client_secret = 'SECRET'

r = praw.Reddit(user_agent=my_user_agent,
                     client_id=my_client_id,
                     client_secret=my_client_secret)

user = r.redditor('REDDITUSERNAME')

for comment in user.comments(limit=None):
    print comment.body 

I get an error every time on the last line, though.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'SubListing' object is not callable

I have connected to the API and have an active connection as I can do print(user.comment_karma) and it displays correctly.

Any ideas what I'm doing wrong?

like image 558
TheBeginningEnd Avatar asked Dec 06 '16 15:12

TheBeginningEnd


1 Answers

According to the documentation, comments is an attribute of the Redditor model in PRAW 4, not a function. Therefore, calling .comments(limit=None) is invalid syntax because .comments isn't a function. Instead, you must specify a listing sort order, like so, because SubListing objects (what user.comments is) inherit from BaseListingMixin:

for comment in user.comments.new():
    print(comment.body)

Admittedly, the documentation for PRAW 4 is very unclear, and you'll probably find the best documentation by searching through the code directly.

like image 142
Aurora0001 Avatar answered Oct 16 '22 04:10

Aurora0001