Last active
August 26, 2019 10:19
-
-
Save amandadebler/d93898fc8605b2617fd762902a28bcfb to your computer and use it in GitHub Desktop.
S&P 500 weights and latest closing prices
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
$response = invoke-webrequest "https://www.slickcharts.com/sp500" | |
$table = $response.parsedhtml.body.childnodes[6].childnodes[1].childnodes[0].childnodes[0].childnodes[1].childnodes[0] | |
# Column Headers | |
$columns = foreach ($node in $table.childnodes[0].childnodes[0].childnodes) {$node.innertext} | |
$stockrows = $table.childnodes[1].childnodes | |
$stocks = foreach ($row in $stockrows) { | |
$attributes = @{} | |
for ($i=0; $i -lt $($columns.count); $i++) { | |
$attributes.Add($columns[$i],$row.childnodes[$i].innertext.trim()) | |
} | |
[PSCustomObject]$attributes | |
} | |
$stocks |
In which I remember the existence of the classic C-style for loop
Usage suggestion: Get-SP500Summary.ps1 | Out-GridView
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Because buying non-US-domiciled ETFs is a bad idea if you're a US citizen, but EU residents can't buy US-domiciled ETFs, and I'm not yet slick enough to use IEX Cloud to figure out how to DIY a mini-S&P 500 fund out of the plain stocks (which we still can buy, either side of the ocean)