Forked from anupkrbid/tickertape-report-exporter.js
Last active
August 19, 2024 07:19
-
-
Save DIVISHAD/c5b47e0b2dfe2ede164353e7dcbaf2d5 to your computer and use it in GitHub Desktop.
This is a script to export the tickertape screener results in a CSV format.
This file contains hidden or 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
| const scrapeData = () => { | |
| const sections = document.querySelectorAll("#screener-table > table"); | |
| const headingElements = sections[0].querySelectorAll("th"); | |
| const scrapedData = Array.from(headingElements).map(elm => { | |
| const selector = elm.getAttribute("id") === "name" ? "data-col" : elm.getAttribute("id") + "-col"; | |
| const rowElements = sections[0].querySelectorAll('tbody')[0].querySelectorAll("." + selector + " .ellipsis .desktop--only") | |
| return { | |
| column: elm.querySelector(".data-cell .desktop--only") ? elm.querySelector(".data-cell .desktop--only").textContent : "#", | |
| rows: Array.from(rowElements).map(el => el.textContent.replaceAll(",","")) | |
| } | |
| }); | |
| return scrapedData; | |
| } | |
| const generateCSVData = scrapedData => { | |
| scrapedData = scrapedData.slice(1, scrapedData.length - 1); | |
| const count = scrapedData[0].rows.length; | |
| // generate structured data | |
| const csvData = Array.from({ | |
| length: count | |
| }, (_, rowIndex) => { | |
| var obj = {}; | |
| scrapedData.forEach(data => { | |
| obj[data.column] = `"${data.rows[rowIndex]}"` | |
| }) | |
| return obj; | |
| }); | |
| const headers = Object.keys(csvData[0]).toString(); | |
| // Get and stringify the keys of the first object in the array | |
| const main = csvData.map(item => Object.values(item).toString()); | |
| // Map finally returns array of arrays of values in each object | |
| const csv = [headers, ...main].join('\n'); | |
| // Creates new array, where first row is keys and further rows the values in each object | |
| return csv; | |
| } | |
| const downloadFile = csvData => { | |
| const anchor = document.createElement('a'); | |
| anchor.href = 'data:text/csv;charset=utf-8,' + encodeURI(csvData); | |
| anchor.target = '_blank'; | |
| anchor.download = `${document.title}`; | |
| anchor.click(); | |
| } | |
| downloadFile(generateCSVData(scrapeData())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment