C# 在C中从HTML表中检索数据#
我想从HTML文档中检索数据。 我正在从一个网站上抓取数据,我几乎完成了,但在尝试从表中检索数据时遇到了问题。 下面是HTML代码C# 在C中从HTML表中检索数据#,c#,html-table,webbrowser-control,C#,Html Table,Webbrowser Control,我想从HTML文档中检索数据。 我正在从一个网站上抓取数据,我几乎完成了,但在尝试从表中检索数据时遇到了问题。 下面是HTML代码 <div id="middle_column"> <form action="url?" method="post" name="inquirydetail"> <input type="hidden" name="ServiceName" value="SurgeWebService"> <input ty
<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>
您对Webbrowser控件中显示的页面没有任何控制权吗?如果这样做,最好为status 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"
}
}
}
但是,这不是一个好方法,因为您要在每个表和每个表中的每个单元格中循环查找文本。将此作为最后一个选项
希望这能帮助您了解情况。我更喜欢使用动态类型和DomeElement属性,但您必须使用.net 4+
对于表,这里的主要优点是不必遍历所有内容。如果您知道要查找的行和列,那么只需按行号和列号将重要数据作为目标,而不是在整个表中循环
另一个最大的优点是,基本上可以使用整个DOM,读取的不仅仅是表的内容。确保在javascript中使用小写属性,即使您使用的是c#
HtmlElement myTableElement;
//使用任何GetElement设置myTableElement。。。方法。
//如果方法返回HtmlElementCollection,请使用循环或方括号索引。
动态myTable=myTableElement.doElement;
for(int i=0;i
谢谢您的回复。本例是搜索整个文档。如果我想开始搜索文档的中间部分,该怎么办?我可以这样做吗?HtmlElement tBody=This.WB.document.GetElementById(“中间列”);HtmlElementCollection tables=tBody.GetElementsByTagName(“表格”);
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"
}
}
}