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

1

5

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?

Cloudberry answered 6/12, 2016 at 15:27 Comment(6)
You shouldn't use code format for error log, meta question about that.Outpour
@Outpour Thanks for the catch. Edited the question as per the meta post.Cloudberry
Are you using PRAW 3 or 4? I believe the API may have changed a little bit since that example.Sweetener
Just a stab in the dark but why do you have just user.comments instead of user.get_comments?Gabor
@Sweetener PRAW 4.Cloudberry
@jeffcarey As of PRAW 4.0 (possibly earlier I'm not sure) get_comments isn't a valid call as it's been replaced with just comments. A lot of the calls that previously used get_something now use just .somethingCloudberry
S
8

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.

Sweetener answered 6/12, 2016 at 15:49 Comment(8)
That's odd, the property definitely exists. Does user = r.redditor('REDDITUSERNAME') help in any way? (instead of get_redditor)Sweetener
Sorry forgot to change that in the code. It is now just r.redditor rather than r.get_redditor, and that is what I'm using I just copy and pasted the code block before fixing that error. I've edited the code in the question now.Cloudberry
Thanks that solved it, although it isn't displaying the full 1000 comment history is should by default - that's what the (limit=none) part did. Any ideas how to add that in if you say it's invalid syntax.Cloudberry
@Cloudberry that should be valid for the .new() function (i.e. user.comments.new(limit=None) should work) but just not for .comments(limit=None) which is invalid.Sweetener
Perfect. I've marked the answer as correct and upvoted it, but because of my karma score the upvote won't show yet.Cloudberry
@Cloudberry you'll get this privilege at 15 rep (you can get 2 more rep points by editing someone's post to improve it) and then you'll have to re-upvote everything (the upvotes aren't actually remembered). Glad it helped you though - PRAW's docs seem really scarce at the minute.Sweetener
@Sweetener I added some examples to the documentation. I hope you find them useful: praw.readthedocs.io/en/latest/code_overview/other/…Katlaps
@Katlaps very helpful, thanks. I appreciate the work you're doing with PRAW 4!Sweetener

© 2022 - 2024 — McMap. All rights reserved.