我想从 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];
}
这里有问题 请帮忙解决这个问题。
最佳答案
您对在 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"
}
}
}
但是,这不是一个好的方法,因为您要遍历每个表格和每个表格中的每个单元格以查找您的文本。将此保留为最后一个选项。
希望这可以帮助您了解。
关于c# - 在 C# 中从 HTML 表中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22950337/