我在使用HTML Agility Pack获取某些特定表时遇到麻烦。我也不能更改实际的HTML,因此不能使用其他ID或类或其他任何内容。
有人可以告诉我如何访问以下每个表吗?
<table class="newTable">
//table 1 contents
<table border="0" cellpadding="3" cellspacing="2" width="100%">
//table 1 - A contents
</table>
</table>
<table border="0" cellpadding="0" cellspacing="0" class="newTable">
//table 2 contents
<table width="100%" border="0" cellspacing="2" cellpadding="0">
//table 2 - A contents
</table>
<table width="100%" border="0" cellspacing="2" cellpadding="0">
//table 2 - B contents
</table>
<table width="100%" cellspacing="2" cellpadding="0">
//table 2 - C contents
</table>
</table>
<table>
//table 3 contents
</table>
现在,如果我要打电话给以下人员
HtmlNode table = doc.DocumentNode.SelectSingleNode("//table");
foreach (var cell in table.SelectNodes("//tr/td"))
{
string someVariable = cell.InnerText
}
我会经历所有事情。我希望能够以不同的方式访问表以关联存储数据的位置。
我试图看类似的东西
doc.DocumentNode.SelectNodes("//table[1]");
但是使用索引似乎不起作用,当我尝试用它指定一个表时,它仍然在所有表中读取或不读取。
同样的事情适用于它,它要么根本不起作用,要么得到所有东西。
foreach (var cell in table.SelectNodes("//table").Skip(some_number))
{
string someVariable = cell.InnerText
}
我正在使用HTML Agility Pack 1.4.9的NuGet包
编辑:
我仅尝试获取表1-A的内容。两者都给出null或endcodingfound异常。
HtmlNode table = doc.DocumentNode.SelectSingleNode("//table/tr/td/table[1]");
HtmlNode table = doc.DocumentNode.SelectSingleNode("//table[1]/tr/td/table[1]");
错误与您的第二次调用有关,“ // tr / td”将返回到根元素。索引器是问题第一部分的正确解决方案,可以通过指定要从您所在的位置进行导航来解决第二部分问题:
HtmlNode table = doc.DocumentNode.SelectSingleNode("//table[1]");
foreach (var cell in table.SelectNodes(".//tr/td")) // **notice the .**
{
string someVariable = cell.InnerText
}
不知道还会发生什么,但是通过将测试表扩展到此代码,以下内容将在我的测试中起作用。这可能意味着您需要共享更多上下文。
这是我用于测试的文档:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<table class="newTable">
<tr>
<td>
<table border="0" cellpadding="3" cellspacing="2" width="100%">
<tr><td>
//table 1 - A contents
</td></tr>
</table>
</td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" class="newTable">
<tr>
<td>
//table 2 contents
<table width="100%" border="0" cellspacing="2" cellpadding="0">
<tr>
<td>
//table 2 - A contents
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="2" cellpadding="0">
<tr>
<td>
//table 2 - B contents
</td>
</tr>
</table>
<table width="100%" cellspacing="2" cellpadding="0">
<tr>
<td>
//table 2 - C contents
</td>
</tr>
</table>
</td>
</tr>
</table>
<table>
<tr>
<td>
//table 3 contents
</td>
</tr>
</table>
</body>
</html>
这是提取您需要的值的代码:
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(text);
var node1A = doc.DocumentNode.SelectSingleNode("//table[1]//table[1]");
string content1A = node1A.InnerText;
Console.WriteLine(content1A);
var node2C = doc.DocumentNode.SelectSingleNode("//table[2]//table[3]");
string content2C = node2C.InnerText;
Console.WriteLine(content2C);
显示:
好的,我使用了您的实际HTML,并且我也得到了NullReference。肯定有些东西会大大混淆敏捷包,不确定为什么。不过,使用Linq API进行一些试验似乎可行,我希望它可以为您提供替代方案:
var table = doc.DocumentNode.DescendantsAndSelf("table").Skip(1).First().Descendants("table").First();
var tds = table.Descendants("td");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句