Reputation: 2799
I've found Fizzler as html parser and jQuery like selector . But seems visual studio can not resolve QuerySelectorAll
method.
here is my code :
using HtmlAgilityPack;
HtmlAgilityPack.HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument document = web.Load(url);
var c = document.QuerySelectorAll('div');
did I miss any references ?
EDIT : after some search I found this code for using Fizzler
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
const string search = "td";
SelectorGenerator<HtmlNode> selectorGenerator1 = new SelectorGenerator<HtmlNode>(new HtmlNodeOps());
HumanReadableSelectorGenerator selectorGenerator2 = new HumanReadableSelectorGenerator();
Parser.Parse(search, new SelectorGeneratorTee(selectorGenerator1, selectorGenerator2));
var c = selectorGenerator1.Selector(Enumerable.Repeat(doc.DocumentNode, 1)).ToList();
I'm still looking for find good answer for my question.
Upvotes: 6
Views: 11218
Reputation: 322
I had same problem and solved it. Maybe you have done same mistake of mine. I had defined assemblies in a few config files. When i deleted all and install packages back again there had been working well.
Fizzler
, Fizzler.Systems.HtmlAgilityPack
in the nuget manager.Install last version of Fizzler
and Fizzler.Systems.HtmlAgilityPack
packages in the manage nuget packages.
After that you can use QuerySelectorAll
and other methods without error wherever you want.
Do not install HtmlAgilityPack only by itself. When you install Fizzler, HtmlAgilityPack will installed automaticly.
Upvotes: 0
Reputation: 521
You are most probably missing reference to Fizzler.Systems.HtmlAgilityPack
I would recommend adding this nuget package. Fizzler
using Fizzler.Systems.HtmlAgilityPack;
using HtmlAgilityPack;
using HtmlDoc = HtmlAgilityPack.HtmlDocument;
and then you can use it like
var html = new HtmlDoc();
html.LoadHtml(HtmlText);
// Fizzler for HtmlAgilityPack is implemented as the
// QuerySelectorAll extension method on HtmlNode
var document = html.DocumentNode;
// yields: [<p class="content">Fizzler</p>]
document.QuerySelectorAll(".content");
Upvotes: 1
Reputation: 9
If using QuerySelectorAll you should creat an array to check if div is not null and then you can use queryselector
HtmlNode[] test= html.DocumentNode.QuerySelectorAll("div.hlogo").ToArray();
for (int rowcounter = 0; rowcounter < test.Count(); rowcounter++)
{
var logoname = test[rowcounter].QuerySelector("a").InnerText;
}
Upvotes: 0
Reputation: 11963
Old question, but that works:
using HtmlAgilityPack;
using Fizzler.Systems.HtmlAgilityPack;
var web = new HtmlWeb();
var document = web.Load("some-url");
var c = document.DocumentNode.QuerySelectorAll("div");
QuerySelectorAll
is an extension method for the HtmlNode
type, but you were trying to use it over a HtmlDocument
.
Upvotes: 5
Reputation: 24344
Probably this:
using Fizzler.Systems.HtmlAgilityPack;
Also give a shot to CsQuery, my C# jQuery port: https://github.com/jamietre/CsQuery
var dom = CQ.CreateFromUrl(url);
var c = dom["div"];
.. plus everything you're used to from jQuery.
Upvotes: 6