|

Automate Your Feed: Build a Simple Reddit Bot with PRAW

3D visualization of a robot managing a stream of Reddit comments and upvotes, representing a Python PRAW bot.

PRAW (Python Reddit API Wrapper) is a fantastic library that makes it easy to interact with Reddit. You can use it to build bots that track keywords, auto-reply to comments, or just scrape data.

Step 1: Get Your Reddit Credentials

This is the hardest part. You must tell Reddit you’re making a bot.

  1. Log in to Reddit and go to Preferences -> Apps.
  2. Click “are you a developer? create an app…”.
  3. Fill it out:
    • Name: MyPythonBot
    • Type: Script
    • Redirect URI: http://localhost:8080 (this doesn’t matter for a script)
  4. Click “create app”. You will get a Client ID (under your bot’s name) and a Client Secret. Save these!

Step 2: Install and Code

pip install praw

Now, let’s write a script that reads the top 5 “hot” posts from the r/learnpython subreddit.

import praw

# 1. Authenticate (use your credentials)
reddit = praw.Reddit(
    client_id="YOUR_CLIENT_ID",
    client_secret="YOUR_CLIENT_SECRET",
    user_agent="MyPythonBot v1.0 by /u/YourUsername", # A descriptive name
    username="YourUsername",
    password="YourPassword"
)

# 2. Choose a subreddit
subreddit = reddit.subreddit("learnpython")

# 3. Get the top posts
print(f"--- Top 5 Hot Posts in r/{subreddit.display_name} ---")
for submission in subreddit.hot(limit=5):
    print(f"Title: {submission.title}")
    print(f"Score: {submission.score}")
    print(f"URL: {submission.url}")
    print("-" * 20)

Challenge

Can you make this bot reply to comments? Be careful! Read Reddit’s API rules. A bot that replies “I love Python!” to every comment that mentions “Python” is a great way to practice (and a great way to get banned if you run it too much!).

Similar Posts

Leave a Reply