Reddit Praw Api usage to search submissions

4.1k views Asked by At

Basic Code:

import praw
r = praw.Reddit(user_agent='Getting the data!!')
r.login("username","password",disable_warning=True)
results=r.search('whatever', subreddit=None, sort=None, syntax=None, period=None)
for x in results:
    print x

I wish to write a code to get all submissions and their comments related. The submissions should be constrained by a search query and time period.

The problems I face is that:

a. I can't understand how to specify the period in the above, the documentation tend to poor

b. I don't know if the result is constrained to a limit. The above code yields:

923 :: Reddit, type with whatever is on your mind no matter how insignificant...
5598 :: Google maps should have a "on the way" feature to find the most conve...
3961 :: LPT: If you're overheating for whatever reason, run your wrists under...
1556 :: As a lad, whenever my mother wanted me to do something and I was play...
5085 :: "THE ENTIRE STATE IS OFFLINE GET IN THERE NOW FIX IT DO WHATEVER IT T...
1259 :: Heyy, I do the webcomic "Subnormality," as well as artwork for Cracke...
604 :: IAMA Professional YouTuber.  Whatever that means... AMA is you'd like
1156 :: [Spoiler] Whatever happened to G2 vs Strix it's an absolute joke
1217 :: Yesterday I ate whatever I wanted and learned something
1291 :: LPT: Set Your Plugins (Flash, etc.) to be activated only with your cl...
1544 :: Whatever you do, don't step on a duck.
1301 :: A diner in Vegas called "Roulette Burger" where each booth has a roul...
649 :: Been trying to establish a very simply basic wardrobe. Not too preppy ...
1141 :: Mods no longer give a shit, post whatever : New Wow expansion doesn't...
549 :: Whatever happened to chatrooms?
212 :: IAmA graphic designer who will spend 5 mins on whatever you want.
673 :: AMA. Hi there, I'm David Ury, I played Spooge in season 2. Please ask ...
0 :: "Whatever they're going to blame on Osama Bin Laden... don't you even be...
3 :: Dinner time! 1/4/15 or 4/1/15 (whatever works)
536 :: Friendly reminder: If you've been given gold, it's perfectly within yo...
378 :: KP, Keratosis Pillaris, "Chicken skin" - whatever you call it, please ...
637 :: [WP]What if we lived in a world where whatever you did to other people...
1053 :: Instead of a gym, have a place where people can go build wood pallets...
69 :: Pick whatever you want Giveaway!
408 :: Just a reminder to newbies. you don't have to buy a whole bitcoin for ...

I highly doubt there must be many more than this. If yes, how can I get them. If the requests are constrained to a time-window. Is there some workaround to sleep and then get more?

c. I don't know if it is constraints like twitter over not accessing historcial data. Though the period argument states opposite. Still not certain.

d.It returns a generator. How can I access the full submission text and related comments' text as well.

Sorry if it seems a bit indirect, but the dearth of examples online and lack of proper documentation has led to face these issues.

2

There are 2 answers

0
goodtimeslim On

A: It's looking for something like '3 months ago'. (You can see some information from API Documentation. it has to be one of (hour, day, week, month, year, all).

B: It is constrained to a limit, it will return about 1000 results max. There might be ways around that, but I'm not sure of what they are, or how easy they are.

C: Kind of the answer to B, I guess.

D: You're looking for the properties of the submission, which you access by attributes (e.g. object.attribute). You can see a full list of attributes for submissions on this page.

So if you wanted to access the self text that goes with a link, you'd have to do something like:

for x in results:
    print x.selftext

If you want to get the comments, though, those are not part of the object. You'd need to get the submission ID and then query the submission itself to get the comments.

1
Nicolaskn On

The default limit is 25 unless another number is specified in the search parameters.

So if you want more results, for example 100 results:

results=r.search('whatever', subreddit=None, sort=None, syntax=None, period=None, **limit=100**)

Commenting on old questions: Because it may help others who look for this in the future.