← Back to list

Retrieving posts, comments, and images from Reddit using Praw in Python 2023.

Reddit is a popular social media platform that allows users to create and share content with other users. As a data scientist or…

Mohanad Diab · 2023-02-19 06:58 · 20 claps · 3.3 min read
#python #reddit #data-science #api #praw
Open on Medium ↗
Wiki topics: ML · Machine Learning 🔒 · Cybersecurity 🔬 · Science · General

Retrieving posts, comments, and images from Reddit using Praw in Python 2023.

Reddit is a popular social media platform that allows users to create and share content with other users. As a data scientist or researcher, you may want to collect information from Reddit to analyze trends, topics, or sentiments. To achieve this, you can use PRAW (Python Reddit API Wrapper), a Python package that provides an interface to interact with Reddit’s API (Application Programming Interface) and retrieve data from the platform.

In this article, we will guide you through the process of retrieving posts and information from Reddit using PRAW in Python.

Installing PRAW.

First, we need to install PRAW using pip, which is the package installer for Python. To install PRAW, open a terminal or command prompt and enter the following command:

pip install praw

This will download and install PRAW and all its dependencies.

Creating a Reddit App.

Before we can use PRAW to interact with Reddit’s API, we need to create a Reddit app and obtain the necessary credentials. To create a Reddit app, you need to have a Reddit account and be logged in, then, follow these steps:

  1. Go to Reddit’s App Preferences page and click on “Create App”. (you’ll find it in the bottom left corner).
  2. Choose the “script” option.
  3. Enter a name and a description for your app (these can be anything you like).
  4. Set the “about url” and “redirect uri” fields to any valid URL (e.g., https://www.example.com).
  5. Click “Create App”.

After you have created the app, you will see a page that shows the app’s Client ID and Client Secret. We will use these credentials to authenticate our app and retrieve data from Reddit’s API.

Figure (1): An example of how the form should look like after you create the app.

Figure (1): An example of how the form should look like after you create the app.

Authenticating the App.

To use PRAW to interact with Reddit’s API, we need to authenticate our app using the credentials we obtained in the previous step. Here’s how to do it:

import praw
import os
import urllib.request

reddit = praw.Reddit(client_id='YOUR_CLIENT_ID',
                     client_secret='YOUR_CLIENT_SECRET',
                     username='YOUR_REDDIT_USERNAME',
                     password='YOUR_REDDIT_PASSWORD',
                     user_agent='YOUR_USER_AGENT')

Replace YOUR_CLIENT_ID, YOUR_CLIENT_SECRET, YOUR_REDDIT_USERNAME, YOUR_REDDIT_PASSWORD, and YOUR_USER_AGENT with the credentials you obtained when you created your Reddit app. Your user agent should be a unique identifier for your app that describes what it does (e.g., "MyBot/1.0") or you can just use your username again.

Retrieving Posts.

Now that we have authenticated our app, we can use PRAW to retrieve posts from Reddit. Let’s start by retrieving the top posts from the “Python” subreddit:

subreddit = reddit.subreddit('python')

for submission in subreddit.top(limit=10):
    print(submission.title)
    print('Score:', submission.score)
    print('Url:', submission.url)
    print()

This code retrieves the top 10 posts from the “Python” subreddit and prints their titles, scores, and URLs. You can replace “python” with the name of any subreddit to retrieve posts from that subreddit, or you can use ‘all’ to access all the subreddits.

Retrieving Comments.

We can also use PRAW to retrieve comments from Reddit. Here’s an example:

submission = reddit.submission(url='https://www.reddit.com/r/python/comments/2xmo63/i_want_to_learn_python_how_should_i_start/')

for comment in submission.comments:
    print(comment.body)
    print('Score:', comment.score)
    print()

This code retrieves the comments from a specific post (specified by its URL) and prints their bodies and scores. You can replace the URL with the URL of any post to retrieve its comments.

Retrieving Images based on a keyword.

We can also use PRAW to retrieve Images from Reddit using a keyword. Here’s an example:

keyword = 'Cat'
subreddit = reddit.subreddit('all')
results = subreddit.search(keyword, limit=200)
save_directory = "enter_your_desired_directory"

for result in results:
    if result.url.endswith('.jpg') or result.url.endswith('.png'):
        try:
            # create the full path to save the image in the directory
            file_path = os.path.join(save_directory, f"{result.id}.jpg")
            urllib.request.urlretrieve(result.url, file_path)
        except urllib.error.HTTPError as e:
            if e.code == 404:
                print('Image not found:', result.url)
            else:
                raise

The above script searches Reddit’s subreddits for posts with the keyword ‘Cat’, then it sets a limit of 200 posts, and specifies a directory to save the photos to.

Then the results are looped through to get images from posts (if they exist, otherwise it will just skip these posts) and saves them to the directory in question.

The script also handles the exception for when an image has been deleted, which is a common exception when downloading image through a url, if not handled this issue will stop the loop early.

For more methods and retrieval options, it is highly advised to go through the Praw documentation in order to familiarize yourself and explore the different potentials of the Reddit API.

See Praw documentation here.

Conclusion.

In conclusion, using the PRAW Python library to retrieve data from Reddit is a straightforward process. By creating a Reddit app using the script option and authenticating it using the credentials obtained, we can retrieve posts, comments, and user information from Reddit’s API. With PRAW’s easy-to-use methods, we can quickly and efficiently gather data from Reddit to perform various tasks, such as analyzing trends, sentiment analysis, or building chatbots. With these tools, developers can explore and interact with one of the most popular online communities in the world.


메타데이터
post_id
a9bd78ff99a
slug
retrieving-posts-comments-and-images-from-reddit-using-praw-in-python-2023-a9bd78ff99a
url
https://medium.com/@mohanaddiab/retrieving-posts-comments-and-images-from-reddit-using-praw-in-python-2023-a9bd78ff99a
canonical_url
https://medium.com/@mohanaddiab/retrieving-posts-comments-and-images-from-reddit-using-praw-in-python-2023-a9bd78ff99a
author_url
https://medium.com/@mohanaddiab
status
ok
fetched_at
2026-06-20 20:29:01