【问题标题】:Loop through <td> elements and define rows / columns循环遍历 <td> 元素并定义行/列
【发布时间】:2013-09-11 04:19:01
【问题描述】:

我有一个 HTML &lt;td&gt; 元素的 HtmlNodeCollection,我使用 HTMLAgilityPack 从表中收集了这些元素。通常,我只会选择表中的 &lt;tr&gt; 元素并循环遍历 &lt;td&gt; 元素,但不幸的是,&lt;tr&gt; 开始标记是通过 JavaScript 生成的,而不是从服务器呈现的。我无法控制 HTML 的呈现方式。因此,我求助于从这个 XPATH 查询中获取 HtmlNodeCollection:

HtmlNode table = htmlDoc.DocumentNode.SelectSingleNode("//table[@width='100%' and @cellpadding='1' and @cellspacing='1' and @border='0']");
HtmlNodeCollection tds = table.SelectNodes(".//td[@align and string-length(@width)=0]"); // only select td elements that have the align attribute and don't have a width attribute

在表格中,有六列和任意数量的行。我想处理每一行并将列解析为中间数据结构。我有获取每个“行”和“列”的代码,但它并不完全正确:

int cols = 6; // six columns
int rows = tds.Count / cols;

// loop through the rows
for (int row = 1; row <= rows; row++)
{
    for (int col = 0; col < cols; col++)
    {
        HtmlNode td = tds[col * row]; // get the associated td element from the column index * row index
        MessageBox.Show(td.InnerHtml + "\n" + td.InnerText);
    }
}

我从第 1 行而不是第 0 行开始,并以行数结束,因为我不想将零乘以六次。我试图将其视为一个矩阵,但我无法定义一行何时结束,下一行何时开始。您对如何正确循环所有行和列有什么建议吗?

【问题讨论】:

    标签: c# html xpath matrix html-agility-pack


    【解决方案1】:

    在纸上画出一个网格后,我很清楚我错过了什么。我需要将列索引添加到列数乘以当前行,如下所示:

    for (int row = 0; row < rows; row++)
    {
        for (int col = 0; col < cols; col++)
        {
            HtmlNode td = tds[col + cols * row]; // get the associated td element from the column index * row index
            MessageBox.Show(td.InnerHtml + "\n" + td.InnerText);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-11
      • 2018-07-11
      • 1970-01-01
      • 2020-03-24
      • 1970-01-01
      • 2012-11-23
      • 2016-05-29
      • 1970-01-01
      • 2018-09-05
      相关资源
      最近更新 更多