在 C# 中从 HTML 表中检索数据

本文关键字:检索 数据 HTML 中从 | 更新日期: 2023-09-27 17:57:25

我想从HTML文档中检索数据。我正在从我几乎完成的网站中抓取数据,但在尝试从表中检索数据时遇到问题。这是 HTML 代码

<div id="middle_column">
<form action="url?" method="post" name="inquirydetail">
    <input type="hidden" name="ServiceName" value="SurgeWebService">
    <input type="hidden" name="TemplateName" value="Inpat_AvailableResponses.htm">
    <input type="hidden" name="CurrentPage" value="inquirydetail">
    <form method="post" action="url" name="ResponseSel" onSubmit="return EditPage(document.forms[3])">    
<TABLE
<tBody
 <table
....
</table
 <table
....
</table
 <table border="0" width="90%">
                    <tr>
                      <td width="10%" valign="bottom" class="content"> Service Number</td>
                      <td width="30%" valign="bottom" class="content"> Status</td>
                      <td width="50%" valign="bottom" class="content"> Status Date</td>
                    </tr>
                    <tr>
                      <td width="20%" bgcolor="white" class="subtitle">1</td>
                      <td width="40%" bgcolor="white" class="subtitle">Approved</td>
                      <td width="40%" bgcolor="white" class="subtitle">03042014</td>
                    </tr>
                    <tr>
                      <td></td>
                    </tr>
                  </table>
</tbody>
</TABle>
</div>

我必须检索状态字段的数据,它已批准并将其写入SQL数据库表单标记中有许多表。表没有 ID。如何获得正确的表、行和单元格这是我的代码

 HtmlElement tBody = WB.Document.GetElementById("middle_column");
   if (tBody != null)
                {
                   string sURL = WB.Url.ToString();
                    int iTableCount = tBody.GetElementsByTagName("table").Count;
                 }
   for (int i = 0; i <= iTableCount; i++)
                    {
                        HtmlElement tb=tBody.GetElementsByTagName("table")[i];
                    }

这里有些不对劲请帮忙。

在 C# 中从 HTML 表中检索数据

您是否无法控制 Web 浏览器控件中显示的页面?如果这样做,最好为状态TD添加一个id字段。那么你的生活会容易得多。

无论如何,以下是在表中搜索值的方法。

HtmlElementCollection tables = this.WB.Document.GetElementsByTagName("table");
            foreach (HtmlElement TBL in tables)
            {
                foreach (HtmlElement ROW in TBL.All)
                {
                    foreach (HtmlElement CELL in ROW.All)
                    {
                        // Now you are looping through all cells in each table
                        // Here you could use CELL.InnerText to search for "Status" or "Approved"
                    }
                }
            }

但是,这不是一个好方法,因为您要遍历每个表和每个表中的每个单元格以查找文本。将此保留为最后一个选项。

希望这能帮助你得到一个想法。

我更喜欢使用动态类型和 DomElement 属性,但您必须使用 .net 4+。

对于表,这里的主要优点是你不必遍历所有内容。如果您知道要查找的行和列,则可以按行号和列号定位重要数据,而不是遍历整个表。

另一个很大的优势是,您基本上可以使用整个 DOM,读取的不仅仅是表的内容。 确保按照 javascript 的要求使用小写属性,即使您使用的是 c#。

HtmlElement myTableElement;
//Set myTableElement using any GetElement...  method.
//Use a loop or square bracket index if the method returns an HtmlElementCollection.
dynamic myTable = myTableElement.DomElement;
for (int i = 0; i < myTable.rows.length; i++)
{
    for (int j = 0; j < myTable.rows[i].cells.length; j++)
    {
        string CellContents = myTable.rows[i].cells[j].innerText;
        //You are not limited to innerText; you have the whole DOM available.
        //Do something with the CellContents.
    }
}