
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.
- Log in to Reddit and go to Preferences -> Apps.
- Click “are you a developer? create an app…”.
- Fill it out:
- Name:
MyPythonBot - Type:
Script - Redirect URI:
http://localhost:8080(this doesn’t matter for a script)
- Name:
- 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 prawNow, 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!).





