(+44) 01438 870220
(+44) 01438 870220

How to use PRAW and crawl Reddit for subreddit post data?


Blog Article

What is PRAW?

PRAW stands for “Python Reddit API Wrapper” and is an easy and fun module to start collecting data from Reddit. The official documentation can be found here:https://praw.readthedocs.io/en/latest/index.html.

Getting started, what you’ll need:

  • A Reddit account is required to access Reddit’s API
  • Basic knowledge of Python 3.6+ 
  • Client ID & Client Secret
    • If you don’t already have a client ID and client secret, follow Reddit’s First Steps Guide to create them.

Set-up & API Registration

Assuming you have a Reddit account already

Step 1:

Visit: https://www.reddit.com/prefs/apps 

Step 2:

At the bottom, you will see “Create App” or similar depending on whether you have existing applications in your account.

Step 3:

Note down your Client ID & Client Secret (image below where to find them).

Step 4:

Install PRAW in Python. PRAW supports Python 3.6+. The recommended way to install PRAW is via pip

pip install praw




Obtaining a Submission instance from a subreddit instance.

You need an instance of the Reddit class to do anything with PRAW. The following will be looking at a “read only” submission instance. In basic terms allowing us to look at submissions in a subreddit as if you were browsing.

Creating our Read-only Reddit Instance

Create a new python file and, using the Client ID and Client Secret, enter your information. The user agent can be left as it is.

import praw

reddit = praw.Reddit(

client_id=”my client id”,

client_secret=”my client secret”,

user_agent=”my user agent” )

To test if your instance is working use:

print(reddit.read_only) # Output: True

Getting data from our chosen subreddit

Choose a subreddit that you want to get submission data for. For my example I’ll use r/pics – where everyone on LinkedIn and Twitter finds their “original” content.

A quick, simple operation – print the submission titles for the top 10 hottest posts right now. In the same python file from above add:

for submission in reddit.subreddit(“learnpython”).hot(limit=10):

print(submission.title)

You should have the top 10 post tiles printed. As seen below:

Using other Submission Attributes

With PRAW we’re able to extract a lot more than just the title posts. Below is my table, I have included others which I typically use. 

Attribute

Description

author

Provides an instance of Redditor.

num_comments

The number of comments on the submission.

score

The number of upvotes for the submission.

title

The title of the submission.

url

The URL the submission links to, or the permalink if a self-post.

How to find all Submission attributes in PRAW

Since attributes are dynamic , there is no a guarantee that attributes seen in my example or other examples will always be present, nor will any list ever really be 100% accurate. The best way to see all available attributes at any given time is to use the following:

import print

# assume you have a Reddit instance bound to variable `reddit`

submission = reddit.submission(id=”39zje0″)

print(submission.title) # to make it non-lazy

pprint.pprint(vars(submission))

Final thoughts

Hopefully this has been an easy introduction to PRAW and using the Submission instance. While there is plenty more you can do from here, such as adding this all into a dataframe and using NLP to uncover sentiment and trends, we’ll leave that for another post. 

If have any questions contact us at hello@honchosearch.com or find me on Linkedin.

Subscribe to our email list to receive blogs post and other Python How-Tos directly to your inbox.

GET IN TOUCH

Find out more about how we can maximise your search marketing performance. Fill in the form below or call us on 01438 870220.

Search Updates September 2021

Search Updates September 2021

Fuzzy Lookup For Mac Using Python and Fuzzywuzzy

Fuzzy Lookup For Mac Using Python and Fuzzywuzzy

The Future of Marketing - three emerging trends that will rewrite how we reach customers

The Future of Marketing - three emerging trends that will rewrite how we reach customers

How To Scrape Youtube Video Views With Python

How To Scrape Youtube Video Views With Python