Search code examples
pythonscreen-scrapingredditpraw

How do I set up and use Praw for Reddit scraping?


I've been trying to use Praw with Python 2.7, and I've been encountering problems. This is the error I've been getting:

Traceback (most recent call last): File "redditscraper.py", line 18, in <module> subreddit = r.get_subreddit(args.subreddit) AttributeError: 'Reddit' object has no attribute 'get_subreddit'

and here is my code:

import praw
import argparse

user_agent = 'PyRedditScraper v0.1 by /u/PizzaFrog'
client_id = 'HIDDEN'
client_secret = 'HIDDEN'
redirect_uri = 'HIDDEN'

parser = argparse.ArgumentParser(description = 'Scrape the specified subreddit')
parser.add_argument('subreddit', help='the subreddit')
args = parser.parse_args()

print 'Scraping /r/' + args.subreddit + '...'
r = praw.Reddit(user_agent = user_agent,
                client_id = client_id,
                client_secret = client_secret,
                redirect_uri = redirect_uri)
subreddit = r.get_subreddit(args.subreddit)
for post in subreddit.get_hot(limit = 10):
    print 'title: ' + post.title
    print 'text: ' + post.selftext
    print 'score: ' + post.score
    print '--------------------'

I have not been able to find any solutions online, any help would be greatly appreciated.


Solution

  • I still have no idea what this issue is from. I downgraded from 4.0.2 to 3.5 and it works fine now.