Categories We Write About

Create a Reddit post scraper

Here’s a simple Python script that scrapes Reddit posts using the praw (Python Reddit API Wrapper) library. This script fetches the top posts from a specified subreddit.


✅ Prerequisites:

  1. Install the praw library:

bash
pip install praw
  1. Create a Reddit app to get your API credentials:
    Go to https://www.reddit.com/prefs/apps → click “create another app” → choose script → fill in name, redirect URI (can be http://localhost), and note down:

    • Client ID

    • Client Secret

    • User Agent (custom string, e.g., MyRedditScraper/0.1)


🔧 Reddit Post Scraper Script

python
import praw # Reddit API credentials reddit = praw.Reddit( client_id='YOUR_CLIENT_ID', client_secret='YOUR_CLIENT_SECRET', user_agent='YOUR_USER_AGENT' ) # Define subreddit and number of posts to scrape subreddit_name = 'technology' post_limit = 10 # Fetch top posts subreddit = reddit.subreddit(subreddit_name) for post in subreddit.top(limit=post_limit): print(f"Title: {post.title}") print(f"Author: {post.author}") print(f"Score: {post.score}") print(f"URL: {post.url}") print(f"Comments: {post.num_comments}") print("-" * 80)

🔄 Customization Options:

  • To scrape from hot, new, or rising:
    Replace subreddit.top(...) with:

    • subreddit.hot(limit=post_limit)

    • subreddit.new(limit=post_limit)

    • subreddit.rising(limit=post_limit)

  • To output to a CSV or JSON file, let me know — I can add that part.


Let me know if you want this adapted for multiple subreddits, date filtering, or keyword search.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

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

Categories We Write About