A Python bot using PRAW (Python Reddit API Wrapper) to scrape Reddit threads and their comments. This tool allows you to extract post data and comments from Reddit threads and save them in various formats.
- Scrape Reddit threads and all their comments
- Export data to JSON, CSV, or console output
- Handle multiple threads at once
- Secure credential management
- Detailed post and comment metadata
- Error handling and logging
- Python 3.6 or higher
- A Reddit account
- Reddit app credentials (see setup instructions below)
Before using this bot, you need to register a Reddit application:
- Log into your Reddit account
- Go to reddit.com/prefs/apps
- Click "create an app" or "create another app"
- Fill in the form:
- Name: Give your app a name (e.g., "My Comment Bot")
- App type: Select "script"
- Description: Optional description
- About URL: Leave blank or add a URL
- Redirect URI: Use
http://localhost:8080(this is just a placeholder)
- Click "create app"
- Important: Copy down your
client_id(the string under "personal use script") andclient_secret
pip install -r requirements.txt-
Copy the template configuration file:
cp config.json.template config.json
-
Edit
config.jsonand replace the placeholder values:{ "client_id": "your_actual_client_id_here", "client_secret": "your_actual_client_secret_here", "user_agent": "Comment Extraction Bot by u/YOUR_REDDIT_USERNAME" }Important: Replace
YOUR_REDDIT_USERNAMEwith your actual Reddit username.
- Never commit your
config.jsonfile to version control - Keep your credentials secure and private
- The
config.jsonfile is already included in.gitignore(if you're using git)
Run the script with default Reddit threads:
python reddit_scraper.pypython reddit_scraper.py --urls "https://www.reddit.com/r/learnprogramming/comments/example/" "https://www.reddit.com/r/AskReddit/comments/example/"Console output only (default):
python reddit_scraper.py --format consoleExport to JSON:
python reddit_scraper.py --format jsonExport to CSV:
python reddit_scraper.py --format csvExport to both JSON and CSV:
python reddit_scraper.py --format both--urls: Space-separated list of Reddit thread URLs to scrape--format: Output format (console,json,csv, orboth)--config: Path to config file (default:config.json)
The JSON output includes:
{
"post": {
"title": "Post title",
"author": "username",
"text": "Post content",
"score": 123,
"upvote_ratio": 0.95,
"num_comments": 45,
"created_utc": "2023-01-01T12:00:00",
"url": "https://...",
"permalink": "https://reddit.com/r/subreddit/comments/...",
"subreddit": "subreddit_name"
},
"comments": [
{
"author": "username",
"body": "Comment text",
"score": 10,
"created_utc": "2023-01-01T12:05:00",
"permalink": "https://reddit.com/r/subreddit/comments/...",
"is_submitter": false,
"parent_id": "t1_..."
}
],
"scraped_at": "2023-01-01T12:10:00"
}The CSV output includes columns:
- Thread (for multiple threads)
- Type (POST or COMMENT)
- Author
- Content
- Score
- Created
- URL
When using JSON or CSV export, files are saved with timestamps:
reddit_data_1_20230101_120000.json- Individual thread datareddit_data_combined_20230101_120000.json- Combined data from all threadsreddit_data_1_20230101_120000.csv- Individual thread datareddit_data_combined_20230101_120000.csv- Combined data from all threads
The script includes comprehensive error handling:
- Invalid URLs are skipped with error messages
- Network issues are handled gracefully
- Missing credentials are detected and reported
- Deleted posts/comments are handled appropriately
Reddit has rate limits for API requests. The script respects these limits automatically through PRAW. For heavy usage:
- Consider adding delays between requests
- Monitor your API usage
- Be respectful of Reddit's resources
-
"Config file not found"
- Make sure you've created
config.jsonfrom the template - Check that the file is in the same directory as the script
- Make sure you've created
-
"Invalid credentials"
- Verify your
client_idandclient_secretare correct - Make sure you're using the "personal use script" ID, not the app name
- Verify your
-
"User agent required"
- Ensure your
user_agentstring is properly formatted - Include your Reddit username in the user agent
- Ensure your
-
"Thread not found"
- Verify the Reddit URL is correct and accessible
- Some threads may be private or deleted
If you encounter issues:
- Check that all dependencies are installed correctly
- Verify your Reddit app credentials
- Test with a simple, public Reddit thread first
- Check Reddit's API status if you're getting unexpected errors
- Respect Reddit's Terms of Service
- Don't scrape private or restricted content
- Be mindful of rate limits and Reddit's resources
- Consider the privacy of users whose content you're scraping
- Use scraped data responsibly and ethically
This project is provided as-is for educational and personal use. Please ensure compliance with Reddit's Terms of Service and applicable laws when using this tool.