5

I'm scraping a number of websites using HtmlAgilityPack. The problem is that it seems to insist on inserting TextNodes in most places which are either empty or just contain a mass of \n, whitespaces and \r.

They tend to cause me issues when I'm counting childnodes , since firebug doesn't show them, but HtmlAgilityPack does.

Is there a way of telling HtmlAgilityPack to stop doing it, or at least clearing out these textnodes? (I want to keep USEFUL ones though). While we're here, same thing for Comment and Script tags.

Aabela
  • 1,408
  • 5
  • 19
  • 28

4 Answers4

2

You can use the following extension method:

static class HtmlNodeExtensions
{
    public static List<HtmlNode> GetChildNodesDiscardingTextOnes(this HtmlNode node)
    {
        return node.ChildNodes.Where(n => n.NodeType != HtmlNodeType.Text).ToList();
    }
}

And call it like this:

List<HtmlNode> nodes = someNode.GetChildNodesDiscardingTextOnes();
Jan Kalfus
  • 5,422
  • 2
  • 30
  • 38
0

There is a difference between "no whitespace" between two nodes and "some whitespace". So all-whitespace textnodes still are needed and significant.

Couldn't you preprocess the html and remove all nodes that you do not need, before starting the "real scraping"?

See also this answer for the "how to remove".

Community
  • 1
  • 1
Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
0

Create an extension method that operates on the "Child" collection (or similar) on a node that uses some LINQ to filter out unwanted nodes. Then, when you traverse your tree do something like this:

myNode.Children.FilterNodes().ForEach(x => {});
Onkelborg
  • 3,927
  • 1
  • 19
  • 22
0

I am looking for a better answer. Here is my current method with respect to childnodes like tables rows and table cells. Nodes are identified by their name TR, TH, TD so I strip out #text every time.

List<HtmlNode> rows = table.ChildNodes.Where(w => w.Name != "#text").ToList();

Sure, it is tedious and works and could be improved by an extension.

Valamas
  • 24,169
  • 25
  • 107
  • 177