Nhóm :
Member
Tham gia:
16-06-2010
Bài viết:
43
Lần thăm:
308

FizzlerEx is a JQuery/CSS3-selectors implementation for .NET

FizzlerEx is a JQuery/CSS3-selectors implementation for .NET, based on HtmlAgilityPack and the original Fizzler project.


using HtmlAgilityPack;
using Fizzler.Systems.HtmlAgilityPack;

var web = new HtmlWeb();
var document = web.Load("http://example.com/page.html")
var page = document.DocumentNode;

foreach(var item in page.QuerySelectorAll("div.item"))
{
    var title = item.QuerySelector("h3:not(.share)").InnerText;
    var date = DateTime.Parse(item.QuerySelector("span:eq(2)").InnerText);
    var description = item.QuerySelector("span:has(b)").InnerHtml;
}

Supported selectors

Original Fizzler project

Selector Description
* All elements
div Elements with the specified tag name
#id Elements with the specified id
.class Elements with the specified class
[attr] Elements with the specified attribute defined
[attr='value'] Elements with the specified attribute name and value
[attr~='word'] Attribute includes the specified word (whitespace-separated)
[attr!='prefix'] Attribute is either equal to 'prefix' or starts with 'prefix' followed by a hyphen (-).
[attr^='prefix'] Attribute starts with 'prefix'
[attr$='suffix'] Attribute ends with 'suffix'
[attr*='search'] Attribute contains 'search'
:first-child Elements that are the first child of their parent
:last-child Elements that are the last child of their parent
:nth-child(n) Elements that are the n-th child of their parent (1-based)
:nth-last-child(n) Elements that are the nth-last-child of their parent (1-based)
:only-child Elements that are the only child of their parent
:empty Elements that have no children
div > p Selects the children of the matched elements
div p Selects the descendant of the matched elements
prev + next

Selects all next elements matching "next" that are immediately preceded by a sibling "prev"

prev ~ siblings Selects all sibling elements that follow after the "prev" element, have the same parent, and match the filtering "siblings" selector.

FizzlerEx additions

Standard JQuery selectors

Selector Description
[attr!='value'] Elements with attribute not equal to value (or without attribute)
:has(b) Elements that contain an element that matches the sub-expression
:not(.class) Elements that do not match the specified sub-expression
:contains('text') Elements whose InnerText contains the specified text
:eq(n) Selects the n-th matched element (zero based)

Non-standard selectors

Selector Description
b:select-parent Selects the parent(s) of the matched node(s)
div[attr=''] Elements without the specified attribute (or with empty value)
/div Performs the initial selection at the top level of the search
context instead of the descendant nodes.
For example, node.QuerySelector("/:select-parent") == node.ParentNode.
Without the slash, the result would be "the parent of the first descendant",
probably not what you want.