Created
April 15, 2013 15:39
-
-
Save tomellis/5389031 to your computer and use it in GitHub Desktop.
Updated for urllib3 for MongoDB for developers course (M101P)
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 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