Search code examples
pythonpraw

How to get the next post from a subreddit using praw?


I was trying to append all 10 posts to a list and then get the next post, but it prints all the post.url before appending to a list. I want to get the next post when the user types something. Any help is appreciated.

Here's my code if you need it:

   posts = submissions.top(limit=10)
   for post in posts:
     url = post.url
     if (url.endswith(('.jpg', '.png', '.gif', '.jpeg'))):
     print(post.url)

Solution

  • First of all, welcome to stack overflow!

    Secondly, you did not really explain what you mean by "I want to get the next post when the user types something.", and your code appears to be wrong, as you did not complete the if statement. I think you want it to be the following:

    posts = submissions.top(limit=10)
    for post in posts:
        url = post.url
        if (url.endswith(('.jpg', '.png', '.gif', '.jpeg'))):
            print(post.url)
    

    From what i understand from you question, you want to print more urls if the user types something. As praw can only fetch a certain amount of posts (max 1000) and that's it (see: https://www.reddit.com/r/redditdev/comments/ckkj11/how_do_i_fetch_more_than_1000_items_using_praw/), i did it as follows:

    allposts = list(submissions.top(limit=1000))
    posts = []
    def next_ten_posts():
        posts.clear
        for x in range(10):
            posts.append(allposts.pop(0))
            loop()
    def loop():
        for post in posts:
            url = post.url
            if (url.endswith(('.jpg', '.png', '.gif', '.jpeg'))):
                print(post.url)
        input()
        next_ten_posts()
    
    next_ten_posts()
    

    That's only how i would do it, there are othre ways to solve the problem. If this does not answer your question, try updating your post