Combining these two allows you to quickly and easily script HTML content.
Installing and Loading AngleSharp
Installing AngleSharp is easy using the
command.
The package is contained in the
library.
Next we will want to load AngleSharp for use in our PowerShell script.
To do this we will want to use theAdd-Typecmdlet to directly load the DLL for the library.
Read on to discover how to parse the webpage content and create a useful PowerShell object!
In this example we will load the content fromInvoke-WebRequestand then using the result, parse the content in AngleSharp.
First, let’s retrieve the weather data.
The HTML structure of the NWS weather page.
Thankfully, there is adivcontainer with an unordered list that we can parse.
The next step is to actually load the retrieved content into AngleSharp.
Very conveniently, the NWS site provides an ID just for this unordered list namedseven-day-forecast-list.
Since each ID is unique on an HTML page, this makes the list easy to target.
Using theAllproperty on our parsed content, we can retrieve just the object with the ID ofseven-day-forecast-list.
There are a handful of elements with classes that we can target.
HTML structure of a single tombstone-container element.
You may notice that theimgtag contains analtproperty with a lot of useful information.
Output of the parsed web page from AngleSharp.
Conclusion
AngleSharp offers an excellent programmatic interface to parsing and interacting with HTML content on webpages.
This can fire up the door to using PowerShell to retrieve content that may be otherwise inaccessible.