Csharp/C#教程:HTML敏捷包分享


HTML敏捷包

我想使用html敏捷包解析html表。 我想从表中只提取一些预定义的列数据。

但我是解析和html敏捷包的新手,我已经尝试但我不知道如何使用html敏捷包来满足我的需求。

如果有人知道,那么尽可能给我一个例子

编辑:

如果我们只想提取决定的列名数据,是否可以解析html表? 就像有4列名称,地址,phno和我想要只提取名称和地址数据。

这里有一个讨论论坛的例子。 向下滚动一下以查看表格答案。 我希望他们能提供更容易找到的更好的样品。

编辑:要从特定列中提取数据,您必须首先找到与所需列对应的

标记并记住它们的索引。 然后,您需要找到相同索引的

标记。 假设您知道列的索引,您可以执行以下操作:

 HtmlDocument doc = new HtmlDocument(); doc.LoadHtml("http://somewhere.com"); HtmlNode table = doc.DocumentNode.SelectSingleNode("//table"); foreach (var row in table.SelectNodes("//tr")) { HtmlNode addressNode = row.SelectSingleNode("td[2]"); //do something with address here HtmlNode phoneNode = row.SelectSingleNode("td[5]"); // do something with phone here } 

Edit2:如果你不知道列的索引,你可以像这样做。 我没有测试过这个。

上述就是C#学习教程:HTML敏捷包分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)

 HtmlDocument doc = new HtmlDocument(); doc.LoadHtml("http://somewhere.com"); var tables = doc.DocumentNode.SelectNodes("//table"); foreach(var table in tables) { int addressIndex = -1; int phoneIndex = -1; var headers = table.SelectNodes("//th"); for (int headerIndex = 0; headerIndex < headers.Count(); headerIndex++) { if (headers[headerIndex].InnerText == "address") { addressIndex = headerIndex; } else if (headers[headerIndex].InnerText == "phone") { phoneIndex = headerIndex; } } if (addressIndex != -1 && phoneIndex != -1) { foreach (var row in table.SelectNodes("//tr")) { HtmlNode addressNode = row.SelectSingleNode("td[addressIndex]"); //do something with address here HtmlNode phoneNode = row.SelectSingleNode("td[phoneIndex]"); // do something with phone here } } } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ctvol.com/cdevelopment/988496.html

(0)
上一篇 2021年12月23日
下一篇 2021年12月23日

精彩推荐