praw icon indicating copy to clipboard operation
praw copied to clipboard

How to get before and after ids from the search response

Open mananwason opened this issue 10 years ago • 5 comments

Hi I am trying to retrieve all the posts from a particular search query. I am using

import praw

 r = praw.Reddit(user_agent='some cool user agent')
posts=r.search(search, subreddit=None,sort=None, syntax=None,period=None,limit=None)
    title=[]
    for post in posts:
        title.append(post.title)
        print post

I am trying to paginate but I am trying to figure out how to retrieve the before and after data and how to use these to get the next page from the data.

mananwason avatar Jun 13 '16 15:06 mananwason

With limit=None, PRAW will automatically collect pages of results during your for loop. There are API limitations on the site's end, however, so your results are probably capping at around 1,000 items or less.

Is that the information you're looking for? The primary way to get around the 1,000 item limit while searching is to make use of the cloudsearch syntax and the timestamp search term.

voussoir avatar Jun 13 '16 22:06 voussoir

@voussoir Thanks for your prompt reply. I am trying to go past the 1000 posts that I am getting right now using praw but there isn't any kind of before and after parameter in it as in the reddit api response. You say that I can get around this limit of 1000 by cloudsearch syntax and the timestamp search term, can you point me to some example? Thanks

mananwason avatar Jun 14 '16 04:06 mananwason

Sure, I've got a little writeup about timestamp searching on the website here, and I have a script that does it through PRAW here. PRAW has a helper function that uses it too.

The hard part about using the timestamp search is including your other search terms. The and and or come before the terms, not in between them. There's are a few examples here. If you have trouble constructing a particular search query I'd be happy to help.

I'm going to close this issue because the 1,000 item limit is an API restriction, and not something that can be fixed with PRAW. Feel free to keep commenting and asking questions, however.

voussoir avatar Jun 14 '16 09:06 voussoir

Thanks @voussoir. I am wondering on how to get more posts? I am able to use cloudsearch but I am restricted by the api limit of 1000 and don't know if there is any pagination parameters here. Please tell me if something can be done to get more than 1000 posts while searching.

mananwason avatar Jun 14 '16 11:06 mananwason

No, as I explained, pagination is handled by PRAW. After the 1,000 items, there are no before or after parameters. If there were, PRAW would use them automatically.

My technique with the timestamp search is to collect all 1,000 posts between two points in time, and then slide the upper and lower bounds to collect 1,000 posts in the next time window. You have to sort the search results by 'new' so that you can use the final result's timestamp as the boundary for your next search query.

Can you share the query you're trying to use? I may be able to give you a better visualization of the process. The timestamp trick may not be applicable to every search query, but it's the only way to get around the 1,000 post cap for this purpose.

voussoir avatar Jun 14 '16 20:06 voussoir