Spaces:
Paused
Paused
feat: Establish initial Munger Engine framework with functional requirements, S&P 500 data fetching, and core stock analysis scripts.
f89d1ea Download scripts/fetch_sp500.ts from dromerosm/munger-engine: direct link, hf CLI and curl.
- Browser
- Download file 1.43 kB
-
https://huggingface.co/spaces/dromerosm/munger-engine/resolve/main/scripts/fetch_sp500.ts
- Command line
-
hf download hf://spaces/dromerosm/munger-engine/scripts/fetch_sp500.ts
-
curl -L -o fetch_sp500.ts https://huggingface.co/spaces/dromerosm/munger-engine/resolve/main/scripts/fetch_sp500.ts
1.43 kB
| import * as cheerio from 'cheerio'; | |
| import fs from 'fs'; | |
| import path from 'path'; | |
| async function getSP500FromWiki() { | |
| const url = 'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'; | |
| console.log('Fetching S&P 500 from:', url); | |
| try { | |
| const res = await fetch(url); | |
| if (!res.ok) throw new Error(`Status: ${res.status}`); | |
| const html = await res.text(); | |
| const $ = cheerio.load(html); | |
| const symbols: string[] = []; | |
| // The first table usually has id="constituents" | |
| const table = $('#constituents'); | |
| if (table.length === 0) { | |
| throw new Error('Could not find constituents table on Wikipedia page.'); | |
| } | |
| // Iterate over rows in the tbody | |
| table.find('tbody tr').each((i, row) => { | |
| // Skip header row if it's th | |
| const firstCell = $(row).find('td').first(); | |
| let symbol = firstCell.text().trim(); | |
| // Yahoo Finance prefers dashes for classes like BRK.B -> BRK-B | |
| if (symbol) { | |
| symbol = symbol.replace(/\./g, '-'); | |
| symbols.push(symbol); | |
| } | |
| }); | |
| console.log(`Successfully extracted ${symbols.length} symbols.`); | |
| // Sample | |
| console.log(symbols.slice(0, 5)); | |
| fs.writeFileSync(path.resolve(__dirname, 'sp500_symbols.json'), JSON.stringify(symbols, null, 2)); | |
| console.log('Saved to scripts/sp500_symbols.json'); | |
| } catch (e: any) { | |
| console.error('Scrape failed:', e.message); | |
| } | |
| } | |
| getSP500FromWiki(); | |