Fetch Reddit Posts using Python

In this tutorial, We will learn How to Fetch Posts From Reddit using Python. We will use the PRAW API (Python Reddit API Wrapper) of Reddit which will help us fetch the posts. This will take input from the user on which topics they want to see the posts and the number of posts they want to see. This will return the title of posts, text of the posts, name of the person who has posted the posts, and number of comments on the post.

Requirements :

Python 3.8 and above
pip install praw
user account on reddit
client Id for API
client secret key for  API

Registration for Reddit API Access :

Code:

# here are all necessary modules
import praw

By Importing the “praw” module of Python, we will able to wrap posts from Reddit. This is the API that is used to fetch posts from Reddit.

# here is the fetch_post function
def fetch_post(search, limit):
    client_id = " " # your client id 
    client_secret = " " # your client secret
    password = " " # your account password
    user_agent = "#app name/1.0 by u/#username"
    username = " " # your account username

    reddit = praw.Reddit(client_id = client_id, 
                     client_secret = client_secret, 
                     password = password, 
                     user_agent = user_agent, 
                     username = username)

    post = reddit.subreddit(search)

    for i in post.hot(limit = limit):
        print(f'Title : {i.title}')
        print(f"text : {i.selftext}")
        print(f"Author : {i.author}")
        print(f"Comments : {i.num_comments}")
        print("----------------------\n")

Here, We will create a fetch_post function that takes two arguments first is the topic that the user wants to see and the second is the limit i.e., the number of posts they want to see. then enter the following details that you got after registering an app like this imagehttps://drive.google.com/file/d/14BhzMNN8jkHgghs_M-aneRolPrAZcBGu/view?usp=sharing, enter your Client ID, Client Secret, the username of your account, password of your account for authentication and create user_agent like this “app_name/version of the app by u/username” ex:- “fetch_post/1.0 by u/xyz”. then we will call a Reddit() function of praw modules and give the arguments we have created above. then call the subreddit function of the Reddict function which will work to get the post related to the topic. then post.hot will fetch the hot post from the topics and return it. we can use other options also like post.new to get the new posts related to the topic or post.top to get the top posts related to the topic. This will return the title of the posts, any text that the post contains, the author name or name of the person who has posted, and number of the comments that posts contain.

# here the function we have created will be called
if __name__ == "__main__":
    search = input("enter what type of post you like to see : ").upper().strip()
    no_of_post = int(input("enter the no. of post want to get : "))
    fetch_post(search, no_of_post)

Here, we will input the name of the topic that the user wants to search and number of the posts they want to see. Then we will pass the argument in the fetch_post function that we have taken the input from the user to get the output.

Complete Code:

import praw

def fetch_post(search, limit):
    client_id = " " # your Client Id you got from reddit
    client_secret = " " # your client Secret you got from reddit
    password = " " # password of reddit account
    user_agent = "#app name/1.0 by u/#username"
    username = "" #your username of reddit account

    reddit = praw.Reddit(client_id = client_id, 
                     client_secret = client_secret, 
                     password = password, 
                     user_agent = user_agent, 
                     username = username)

    post = reddit.subreddit(search)

    for i in post.hot(limit = limit):
        print(f'Title : {i.title}')
        print(f"text : {i.selftext}")
        print(f"Author : {i.author}")
        print(f"Comments : {i.num_comments}")
        print("----------------------\n")
    
if __name__ == "__main__":
    search = input("enter what type of post you like to see : ").upper().strip()
    no_of_post = int(input("enter the no. of post want to get : "))
    fetch_post(search, no_of_post)

This Python code uses the praw module of Python to fetch the post from Reddit. The user has to give the input the name of the topic and the number of posts they want to see. you must have an account on Reddit and Client ID, Client Secret to access the API.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top