Created
June 5, 2017 11:46
-
-
Save justindavies/8a944b56cfc320441d74a8e03d576f1c to your computer and use it in GitHub Desktop.
Import NASDAQ companies data into Mongo or Cosmos DB
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 pymongo | |
from pymongo import MongoClient | |
import urllib2 | |
import csv | |
# Are we using development or Production ? | |
# uri = "mongodb://USERNAME:PASSWORD@INSTANCE.documents.azure.com:10255/?ssl=true&replicaSet=globaldb" | |
uri = 'mongodb://localhost:27017' | |
# Connect to the instance and get a handle | |
client = MongoClient(uri) | |
db = client.fluid | |
companies = db.companies | |
# Let's pull the NASDAQ data directly | |
url = 'http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NASDAQ&render=download' | |
csvfile = urllib2.urlopen(url) | |
# Read the CSV into a dict so we can pipe it into DB | |
reader = csv.DictReader(csvfile, delimiter=',') | |
# And import - I'm not too worried about batching these as it's only a few thousand entries | |
for line in reader: | |
print("Inserting " + line['Name']) | |
companies.insert_one(line) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment