Class HtmlNodeExtensions
Extension methods for finding elements within with IEnumerable<HtmlNode>
Inheritance
Namespace: IronWebScraper
Assembly: IronWebScraper.dll
Syntax
public static class HtmlNodeExtensions : Object
Filtering a set of DOM nodes by text or by a further CSS selector runs through the HtmlNodeExtensions methods. They are extension methods on IEnumerable<HtmlNode>, so they chain directly onto the array a Response.Css or HtmlNode.Css query returns, narrowing a result set without a manual loop.
ContainingText(search) keeps only the nodes whose text contains the search string, and ContainingTextCaseInsensitive(search) does the same while ignoring case. CSS(selector) runs an additional CSS selector across every node in the set and returns the combined matches, acting as the set-level counterpart to the selector query on a single node. Because the text filters return a plain IEnumerable<HtmlNode>, several can be chained to narrow a large match set step by step before the fields are read, and they compose with LINQ operators such as Where and Select.
HtmlNode[] saleItems = response.QuerySelectorAll("div.product").ContainingText("Sale").ToArray();The shopping-site how-to filters product nodes this way, and the web scraping tutorial covers selecting and narrowing elements.
Methods
ContainingText(IEnumerable<HtmlNode>, String)
Returns all HtmlNodes in the collection that contain the search text.
Declaration
public static IEnumerable<HtmlNode> ContainingText(this IEnumerable<HtmlNode> nodes, string search)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<HtmlNode> | nodes | The nodes. |
| System.String | search | The search string. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<HtmlNode> |
ContainingTextCaseInsensitive(IEnumerable<HtmlNode>, String)
Returns all HtmlNodes in the collection that contain the search text. Case Insensitive.
Declaration
public static IEnumerable<HtmlNode> ContainingTextCaseInsensitive(this IEnumerable<HtmlNode> nodes, string search)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<HtmlNode> | nodes | The nodes. |
| System.String | search | The search string. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<HtmlNode> |
CSS(IEnumerable<HtmlNode>, String)
Searched multiple HtmlNodes for the given CSS Selector, and returns all distinct matches.
Declaration
public static HtmlNode[] CSS(this IEnumerable<HtmlNode> nodes, string selector)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<HtmlNode> | nodes | HtmlNodes to search |
| System.String | selector | A CSS query selector |
Returns
| Type | Description |
|---|---|
| HtmlNode[] |