How to Keep Track of Top Posts on Reddit with Free API

import requests
import pandas as pd
subreddit = ['30PlusSkinCare','Skincare_Addiction','AsianBeauty','MakeupAddiction']
limit = 400
timeframe = 'week' #hour, day, week, month, year, all
listing = 'best' # controversial, best, hot, new, random, rising,top
  1. Import packages.
  • Top is the raw score, upvotes minus downvotes.
  • Best is more about the proportion of upvotes to downvotes.
  • Controversial gives high scores to posts that have a lot of both upvotes and downvotes — a lot of people like it but a lot of people also dislike it.
  • Hot are posts that have gotten a lot of votes recently, either up or down, so it shows relatively new posts that are getting attention.
  • Rising is what is getting a lot of activity (comments/upvotes) right now.
  • New sorts post by the time of submission with the newest at the top of the page.
def get_reddit(subreddit,listing,limit,timeframe):
try:
base_url = f'https://www.reddit.com/r/{subreddit}/{listing}.json?limit={limit}&t={timeframe}'
request = requests.get(base_url, headers = {'User-agent': 'yourbot'})
except:
print('An Error Occured')
return request.json()
def get_results(r):

myDict = {}
for post in r['data']['children']:
myDict[post['data']['title']] = {'url':post['data']['url'],'score':post['data']['score'],'comments':post['data']['num_comments']}
df = pd.DataFrame.from_dict(myDict, orient='index')
return df
df_f=pd.DataFrame()for i in range(len(subreddit)):
result=get_reddit(subreddit[i],listing,limit,timeframe)
df = get_results(result)
df_f=df_f.append(df)
df_f=df_f.reset_index()
df_f

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Chi

Chi

263 Followers

Books | Marketing | Data Viz | Analytics & Experimentation | Entrepreneurship 💡Founder of beautydupes.xyz | Blog: goodmarketing.club