Created
January 5, 2021 17:19
-
-
Save toinetoine/fde1054c2c7a1db6b91a3a1e8b2e727e to your computer and use it in GitHub Desktop.
grab list of s&p500 companies
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
// https://en.wikipedia.org/wiki/List_of_S%26P_500_companies | |
var dataFieldKeys = []; | |
var companies = []; | |
var domTable = document.getElementById('constituents'); | |
// populate col headers | |
var domTableHeaders = domTable.children[0].children[0].children; | |
for(var i = 0; i < domTableHeaders.length; i++) { | |
if (domTableHeaders[i].children.length == 0) { | |
dataFieldKeys.push(domTableHeaders[i].innerHTML.trim()); | |
} else { | |
dataFieldKeys.push(domTableHeaders[i].children[0].innerHTML.trim()); | |
} | |
} | |
// read each row in the table body | |
var domTableRows = domTable.children[1].children; | |
for (var rowI = 0; rowI < domTableRows.length; rowI++) { | |
var domRowTds = domTableRows[rowI].children; | |
if (domRowTds.length != dataFieldKeys.length) { | |
console.error('Mismatch number of cols with this row\'s number of cols' + domTableRows.innerHTML); | |
break; | |
} | |
var company = {}; | |
// populate company object with this row | |
for (var colI = 0; colI < domRowTds.length; colI++) { | |
if (domRowTds.length > 0) { | |
company[dataFieldKeys[colI]] = domRowTds[colI].children[0].innerHTML.trim() | |
} else { | |
company[dataFieldKeys[colI]] = domRowTds[colI].innerHTML.trim(); | |
} | |
} | |
companies.push(company); | |
} | |
console.log(JSON.stringify(companies)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment