Created
September 26, 2016 17:29
-
-
Save kbolt/e9ab0fdee44078901ad9139101e948f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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