Skip to content

Instantly share code, notes, and snippets.

@tomellis
Created April 15, 2013 15:39
Show Gist options
  • Save tomellis/5389031 to your computer and use it in GitHub Desktop.
Save tomellis/5389031 to your computer and use it in GitHub Desktop.
Updated for urllib3 for MongoDB for developers course (M101P)
import json
import urllib3
import pymongo
# connect to mongo
connection = pymongo.Connection("mongodb://localhost", safe=True)
# get a handle on the reddit db
db=connection.reddit
stories = db.stories
# get the reddit homepage
http_pool = urllib3.HTTPConnectionPool('www.reddit.com')
reddit_page = http_pool.urlopen('GET', '/r/technology/.json', redirect=True)
# parse the json into python objects
parsed = json.loads(reddit_page.data)
# interate through every news item on the page
for item in parsed['data']['children']:
# put it in mongo
stories.insert(item['data'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment