简体   繁体   中英

HtmlWebResponseObject.ParsedHtml replacement in Powershell Core 6

My goal is to parse an html file retrieved with Invoke-WebRequest . If possible I'd like to avoid any external libraries.

The problem I am facing is, that Invoke-WebRequest returns a BasicHtmlWebResponseObject instead of a HtmlWebResponseObject since Powershell 6 . The Basic version misses the ParsedHtml property. Is there a good alternative to parse html in Powershell Core 6?

I've tried to use Select-Xml but my html is not entirely valid (eg a missing closing tag), hence this fails to parse the result.

Another alternative I've found is to use New-Object -ComObject "HTMLFile" but from my understanding this relies on Internet Explorer for parsing which I'd like to avoid.

There is a very similar question here but sadly this question had no answer or activity since 8 months.

As mentioned in the comments it is not really possible without a library. One very good library you could use it the AngleSharp library for dotnet. It has great html parsing capabilities and dotnet code interacts very friendly with powershell, have a look at this link .

Here is an example from their website:

var config = Configuration.Default.WithDefaultLoader();
var address = "https://en.wikipedia.org/wiki/List_of_The_Big_Bang_Theory_episodes";
var context = BrowsingContext.New(config);
var document = await context.OpenAsync(address);
var cellSelector = "tr.vevent td:nth-child(3)";
var cells = document.QuerySelectorAll(cellSelector);
var titles = cells.Select(m => m.TextContent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM