【问题标题】:Get nested table cell with HtmlAgilityPack使用 HtmlAgilityPack 获取嵌套表格单元格
【发布时间】:2012-09-11 09:24:22
【问题描述】:

我有以下 html 页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

<head>
</head>

<body>
<div style="LEFT: 135; WIDTH: 450; POSITION: absolute; TOP: 70">
  <a name="content_begins"></a>
  <table cellSpacing="0" cellPadding="0" width="100%" border="0">
    <tr>
      <td vAlign="top">
      <table class="PortalGadget" cellSpacing="0" cellPadding="0" width="100%" border="0">
        <tr>
          <td>
          <table cellSpacing="6" cellPadding="0" border="0">
            <form name="authenticator" ACTION="auth-cup" METHOD="POST" ENCTYPE="application/x-www-form-urlencoded">
              <tr bgColor="#6c8899">
                <td><font face="Trebuchet MS" color="#ffffff">&nbsp;*** You Have Already Logged In ***</font></td>
              </tr>

              <tr height="20">
                <td vAlign="bottom" height="20">
                <font face="Trebuchet MS" size="-1">Context:</font></td>
              </tr>
              <tr>
                <td></td>
              </tr>
              <tr>
                <td vAlign="bottom" height="20">
                <font face="Trebuchet MS" size="-1">Username:</font></td>
              </tr>
              <tr>
                <td><input size="40" name="username"></td>
              </tr>
              <tr>
                <td><font face="Trebuchet MS" size="-1">Password:</font></td>
              </tr>
              <tr>
                <td><input type="password" size="40" name="password"></td>
              </tr>
              <tr>
                <td vAlign="bottom" height="20">
                <font face="Trebuchet MS" size="-1">Destination:</font></td>
              </tr>
              <tr>
                <td>
                <input size="40" name="url" value="http://ya.ru/"></td>
              </tr>
              <tr>
                <!---- Hidden field to identify forward/reverse proxy path ------>
                <input TYPE="hidden" NAME="proxypath" VALUE="forward">
              </tr>
              <tr>
                <td vAlign="bottom" align="left">
                <input type="submit" alt="Login" value="Login" border="0" name="loginButton">&nbsp;&nbsp;
                <input type="reset" alt="Reset" value="Reset"></td>
              </tr>
              <tr height="5">
                <td height="5"><hr SIZE="2"></td>
              </tr>
              <tr>
                <td><font face="Trebuchet MS" size="-1">
                <p>Copyright 1999-2003 Novell, Inc. All rights reserved.</p>
                </font></td>
              </tr>
            </form>
            </tbody>
          </table>
          </td>
        </tr>
      </table>
      </td>
    </tr>
  </table>
</div>
</body>
</html>

我想从 td 获取文本“您已经登录”。我有这个代码:

Stream str = SendData().GetResponseStream();
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.Load(str);
            foreach (HtmlAgilityPack.HtmlNode table in doc.DocumentNode.SelectNodes("//table[@class='PortalGadget']"))
            {
                MessageBox.Show("Found: " + table.Id);
                foreach (HtmlAgilityPack.HtmlNode row in table.SelectNodes("//table"))
                {

                    HtmlAgilityPack.HtmlNodeCollection cells = row.SelectNodes("tr");

                    if (cells == null)
                    {
                        continue;
                    }

                    foreach (HtmlAgilityPack.HtmlNode cell in cells)
                    {
                        MessageBox.Show("cell: " + cell.InnerText);
                    }
                }
            }

但我无法在代码中显示“您已登录”文本。在这种情况下如何获取嵌套表格单元格的值?

【问题讨论】:

    标签: c# html-parsing html-agility-pack


    【解决方案1】:

    嗯,在我看来,我找到了解决方案:

    string value = doc.DocumentNode.SelectNodes("//table[@class='PortalGadget']/tr/td/table/tr/td")[0].InnerText;
    

    【讨论】:

      【解决方案2】:

      要从另一个节点中选择节点,您必须输入一个 '.'在 XPath 的开头。

      所以table.SelectNodes("//table") 变成了table.SelectNodes(".//table")

      【讨论】:

        【解决方案3】:

        这实际上取决于您认为该数据的判别性,但是当我查看 HTML 时,似乎 BGCOLOR 设置为#ffffff 的 FONT 元素是,所以这是相应的 Html Agility Pack 代码:

        HtmlDocument doc = new HtmlDocument();
        doc.Load(yourHtmlFile);
        HtmlNode node = doc.DocumentNode.SelectSingleNode("//font[@color='#ffffff']");
        Console.WriteLine(node.InnerText.Trim());
        

        将输出:

        &nbsp;*** You Have Already Logged In ***
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-07-06
          • 1970-01-01
          • 2021-05-29
          • 2010-11-23
          • 1970-01-01
          • 1970-01-01
          • 2015-05-08
          • 2015-02-14
          相关资源
          最近更新 更多