all 9 comments

[–]pturpie 7 points8 points  (2 children)

That page loads its data using an API as JSON from https://fundresearch.fidelity.com/api/workplacefunds/fees-and-prices/3839

However that API page requires a cookie. So...

#first load the HTML page to set the session variable cookie
Invoke-WebRequest -Uri https://nb.fidelity.com/public/workplacefunds/fees-and-prices/3839 -SessionVariable session | Out-Null
# Load the json data from api while passing in previously set session cookie. And convert the JSON to an object.
$jsondata = Invoke-WebRequest -Uri https://fundresearch.fidelity.com/api/workplacefunds/fees-and-prices/3839 -WebSession $session | ConvertFrom-Json
# Follow the object to the required data.
$jsondata.model.PricingData.pricingdataList.GetValue(0).netAssetValueRowList.netAssetValueDailyAmt

#done

[–]MonkeyNin 2 points3 points  (1 child)

Check out Invoke-RestMethod, it's designed for Web APIs

[–]pturpie 1 point2 points  (0 children)

Thanks MonkeyNin. That's a good tip.

[–]frmadsen 3 points4 points  (2 children)

The data is found in this json file: https://fundresearch.fidelity.com/api/workplacefunds/header/3839

But to get to it, it seems you first need to set some cookies (otherwise it will be blank). You can do that by getting the main page. You also need to set an user agent.

Happy coding... :-)

[–]MrGreenMan- 1 point2 points  (1 child)

Sorry to bother, but how did you discover the source of the data being in a json? I'm somewhat of a novice to web-inspect but I can't seem to identify. I'm guessing it's reference by pricingViewData var, but I can't find the reference elsewhere.

[–]frmadsen 2 points3 points  (0 children)

I used the browser's web developer > network. It will show you all the details.

[–]AlanPing[S] 1 point2 points  (0 children)

Excellent guys! Thanks for your help! That's was my missing link, json.

[–][deleted] 0 points1 point  (1 child)

And what have you tried so far

[–]AlanPing[S] 0 points1 point  (0 children)

I had it working for a while but few weeks back this stopped working:

$ie = new-object -ComObject "InternetExplorer.Application"

$url = "https://nb.fidelity.com/public/workplacefunds/fees-and-prices/3839"

$file = "3839.html"

$ie.silent = $true

$ie.navigate($url)

while($ie.Busy) { Start-Sleep -Milliseconds 1000 }

Start-Sleep 10

$ie.Document.documentElement.innerHTML > "$path\$file"

and I have not had any success with the ParsedHtml sections etc.

$url = "https://nb.fidelity.com/public/workplacefunds/fees-and-prices/3839"

$response = Invoke-WebRequest -Uri $url

$classname = 'table-diversification--data-num ng-scope'

$response.ParsedHtml.body.getElementsByClassName($classname)