How to use python with reddit - the correct way

How to use python with reddit – the correct way

I came to a realization shortly after gushing about the reddit karma crawler script in my last post.

Actually two.

That script does not work. Basically, it relies on old code that simply is not there on the reddit page anymore

That script sucks. If you want to scrape a website, don’t use a full browser automation like Selenium.

What gives, old man?

Simply, I was too excited about the concept of the script and did not do my research properly before posting.
Well, I am making up for that now.

How to connect to reddit the proper way

Reddit provides an official reddit API with ridiculously lax rules.

And there are simple wrappers for a lot of languages. It is a long list of goodness.

Seriously, using PRAW – the reddit wrapper for python – this will get you the 5 hottest posts from the opensource subreddit.

import praw
r = praw.Reddit(user_agent=’my_cool_application’)
submissions = r.get_subreddit(‘opensource’).get_hot(limit=5)
[str(x) for x in submissions]

Here is the PRAW documentation and a nice tutorial from HumanGeo.

For those needing to (or chosing to) use PHP, here is your PHP reddit wrapper library.

Using these, rebuilding the karma link crawler script (and dozens of other evil genius things) should be quite easy.