HtmlAgilityPack - Grab data from html table

穿精又带淫゛_ 提交于 2019-12-02 02:11:09
Hans

The following XPATH allows you to search for a specific DIV (with the class 'boardcontainer') within your HTML document:

//div[@class='boardcontainer']/table

To handle empty rows, simply check whether or not the returned HtmlNodeCollection is null.

Here is a complete example:

HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);

foreach (HtmlNode table in htmlDoc.DocumentNode.SelectNodes("//div[@class='boardcontainer']/table"))
{
  Console.WriteLine("Found: " + table.Id);

  foreach (HtmlNode row in table.SelectNodes("tr"))
  {
    Console.WriteLine("row");

    HtmlNodeCollection cells = row.SelectNodes("th|td");

    if (cells == null)
    {
      continue;
    }

    foreach (HtmlNode cell in cells)
    {                        
      Console.WriteLine("cell: " + cell.InnerText);
    }
  }
} 

You should also check if a table is found and if the found table contains rows at all.

Try:

foreach (HtmlNode table in 
         htmlDoc.DocumentNode.SelectNodes("//div[@class='boardcontainer']/table"))

It's an XPath expression matching the attribute. See here for more info:

http://www.exampledepot.com/egs/org.w3c.dom/xpath_getelembyattr.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!