Skip to content

Instantly share code, notes, and snippets.

@amandadebler
Last active August 26, 2019 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amandadebler/d93898fc8605b2617fd762902a28bcfb to your computer and use it in GitHub Desktop.
Save amandadebler/d93898fc8605b2617fd762902a28bcfb to your computer and use it in GitHub Desktop.
S&P 500 weights and latest closing prices
$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
@amandadebler
Copy link
Author

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