【问题标题】:Retrieve data from HTML table in C#从 C# 中的 HTML 表中检索数据
【发布时间】:2014-05-21 22:25:27
【问题描述】:

我想从 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 DB 表单标签中有很多表格。表格没有 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-table webbrowser-control


    【解决方案1】:

    您对 Webbrowser 控件中显示的页面没有任何控制权吗?如果你这样做,最好为状态 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"
                        }
                    }
                }
    

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

    希望这可以帮助您获得一个想法。

    【讨论】:

    • 感谢您的回复。此示例正在搜索整个文档。如果我想开始搜索文档中间怎么办。我可以这样做 HtmlElement tBody =this.WB.Document.GetElementById("middle_column") ;HtmlElementCollection 表格 = tBody.GetElementsByTagName("table");
    【解决方案2】:

    我更喜欢使用动态类型和 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.
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      相关资源
      最近更新 更多