我正在尝试从包含 2-6 个带有 HtmlAgilityPack 的表的 DIV 中提取信息。
只要有相同数量的表,我就可以通过使用毫无问题地获取信息
var thirdTable = res.SelectSingleNode("table[3]");
每个表都包含 tds,第一个表包含我要检查的名称。目前它是由
<table border="0" cellspacing="1" cellpadding="4" width="100%">
<tbody>
<tr bgcolor="#505050">
<td colspan="2" class="white">
<b>Character Deaths</b>
</td>
</tr>
</tbody>
</table>
var charTest = thirdTable.ChildNodes[0].InnerText.StartsWith(checkInfo);
但由于表的数量不时不同,我无法在没有 NullReferenceException 的情况下提取信息。
我如何检查表的数量然后告诉存在哪些表?
编辑:
var URLCharacter = "http://www..." + tbSearch.Text;
HtmlAgilityPack.HtmlWeb doc = new HtmlAgilityPack.HtmlWeb();
htmldocObject = doc.Load(URLCharacter);
//Find the right contentdiv
var res = htmldocObject.DocumentNode.SelectSingleNode("//div[@class='BoxContent']");