Skip to content

Instantly share code, notes, and snippets.

@kbolt
Created September 26, 2016 17:29
Show Gist options
  • Save kbolt/e9ab0fdee44078901ad9139101e948f6 to your computer and use it in GitHub Desktop.
Save kbolt/e9ab0fdee44078901ad9139101e948f6 to your computer and use it in GitHub Desktop.
import praw
import requests
import sqlite3
# A simple scraper for grabbbing images from the front page of a given subreddit and storing them in a DB
# Create database
conn = sqlite3.connect('imgdb.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS images
(title text, url text)''')
def grab_images(query):
BASE_URL = "http://www.reddit.com"
user_agent = ("imagefinder by ..."
"")
r = praw.Reddit(user_agent=user_agent)
subreddit = r.get_subreddit(query)
url = BASE_URL + '/r/' + query
response = requests.get(url)
posts = subreddit.get_hot(limit=20)
print("Grabbing images from /r/" + query)
for s in posts:
link = s.url
title = s.title
if "imgur.com" in link:
data = [(title, link)]
c.executemany('INSERT INTO images VALUES (?,?)', (data))
conn.commit()
print("Operation completed!")
grab_images('wallpaper')
c.close()
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment