Skip to content

Instantly share code, notes, and snippets.

@yongghongg
Last active April 17, 2022 14:35
Show Gist options
  • Save yongghongg/aa5a70bc7e307165ac6a2767faf7f98d to your computer and use it in GitHub Desktop.
Save yongghongg/aa5a70bc7e307165ac6a2767faf7f98d to your computer and use it in GitHub Desktop.
# import packages
import pandas as pd
from pymongo import MongoClient
# scrape the list of S&P 500
url = 'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
payload = pd.read_html(url)
symbol_list = payload[0]
# save to local csv file
symbol_list.to_csv('SAP500_symbol_list.csv', index=False)
# push symbol list to MongoDB
# establish a client connection to MongoDB
MONGODB_CONNECTION_STRING = "<Your Connection String Here>"
client = MongoClient(MONGODB_CONNECTION_STRING)
# convert to dictionary for uploading to MongoDB
symbol_dict = symbol_list.to_dict('records')
# point to symbolsDB collection
db = client.symbolsDB
# emtpy symbols collection before inserting new documents
db.symbols.drop()
# insert new documents to collection
db.symbols.insert_many(symbol_dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment