CSS selector in dotnetcore?

.net-core c# html-agility-pack

Question

I was using Fizzler.Systems.HtmlAgilityPack; in .NET to get elements using CSS selectors. Now I'm porting my project over to .NET core and there doesn't seem to be a Fizzler although HtmlAgilityPack.NetCore is available. How do I use CSS selectors?


Popular Answer

For CSS selectors in .NET, I always used ScrapySharp (although it does not support pseudo-elements).

Add ScrapySharp.Extensions to your using statements, and all you need is to invoke CssSelect in any HtmlNode object, such as DocumentNode.

using ScrapySharp.Extensions;
using HtmlAgilityPack;

namespace ConsoleLab
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            HtmlWeb web = new HtmlWeb();
            var document = web.Load("your url");
            //css class selector example
            var res1 = document.DocumentNode.CssSelect(".yourClass");
            //css id selector example
            var res2 = document.DocumentNode.CssSelect("#yourID");
        }
    }
}

An alternative solution is to use AngleSharp - which is an all-in-one package for parsing and css-selecting (css selectors are built in). It's been a while since I use these, but if I am not wrong angle sharp offers better css selector support.

Anglesharp usage examples:

    //parsing a http-served url (asynchronous, used .Result here for simplification, but this defeats the asynchronoability of the code)
    IBrowsingContext bc = BrowsingContext.New();
    Task<IDocument> doc = bc.OpenAsync("yourAddress");
    //querying a single selector match
    IElement element1 = doc.Result.QuerySelector(".yourSelector");
    //querying multiple selector matches
    IEnumerable<IElement> elements1 = doc.Result.QuerySelectorAll(".yourSelectors");

    //parsing a physical html document, non-network dependent
    HtmlParser parser = new HtmlParser();
    IHtmlDocument doc2 = parser.Parse("htmlFile");
    IElement element2 = doc.Result.QuerySelector(".yourSelector");
    IEnumerable<IElement> elements2 = doc.Result.QuerySelectorAll(".yourSelectors");


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why