Get Hot Posts From Subreddit
Tags: #reddit #subreddit #data #hottopics #rss #information
This notebook explains how to get hot posts from a subreddit. A subreddit is a specific online community, and the posts associated with it, on the social media website Reddit

Input

Install packages

1
!pip install praw
Copied!
1
import praw
2
import pandas as pd
3
import numpy as np
4
from datetime import datetime
Copied!

Choose Subreddit topic

1
SUBREDDIT = "Python" #example: "CryptoCurrency"
Copied!

Setup App to connect to Reddit API

  • To get data from reddit, you need to create a reddit app which queries the reddit API.
  • Select “script” as the type of app.
  • Name your app and give it a description.
  • Set-up the redirect uri to be http://localhost:8080.
  • Once you click on “create app”, you will get a box showing you your "client_id" and "client_secrets".
  • "user_agent" is the name of your app.
If you need help on setting up and getting your API credentials, please visit ---> Get Reddit API Credentials
1
MY_CLIENT_ID = 'EtAr0o-oKbVuEnPOFbrRqQ'
2
MY_CLIENT_SECRET = 'LmNpsZuFM-WXyZULAayVyNsOhMd_ug'
3
MY_USER_AGENT = 'script by u/naas'
Copied!

Model

Connect with the reddit API
1
reddit = praw.Reddit(client_id=MY_CLIENT_ID, client_secret=MY_CLIENT_SECRET, user_agent=MY_USER_AGENT)
Copied!
Get the subreddit level data
1
posts =[]
2
for post in reddit.subreddit(SUBREDDIT).hot(limit=50):
3
4
posts.append([post.title, post.score, post.id, post.subreddit, post.url, post.num_comments, post.selftext, post.created])
5
posts = pd.DataFrame(posts,columns=['title', 'score', 'id', 'subreddit', 'url', 'num_comments', 'body', 'created'])
Copied!
  • If you need more variables, check "vars()" function``
  • Usage: 'vars(post)', you'll get post level variables
Convert unix timestamp to interpretable date-time
1
posts['created']=pd.to_datetime(posts["created"],unit='s')
Copied!

Output

1
posts.head()
Copied!
Hint: Filter data using "created" variable for past 24 hours hot posts

Additional Resources

  • More info on the PRAW package used: https://praw.readthedocs.io/en/stable/
Last modified 2mo ago
Copy link
Edit on GitHub