Htmlagilitypack 仅部分解析 table 行

Htmlagilitypack only parses table rows partialy

我正在尝试解析主要内容(dom 树中的最后一个)

<table>

在此网站中:“https://aips.um.si/PredmetiBP5/Main.asp?Mode=prg&Zavod=77&Jezik=&Nac=1&Nivo=P&Prg=1571&Let=1” 我在 visual studio 17.

的 wpf 应用程序上使用 Htmlagilitypack 并在 C# 中编写代码

我现在正在使用此代码:

iso = Encoding.GetEncoding("windows-1250");
web = new HtmlWeb()
{
    AutoDetectEncoding = false,
    OverrideEncoding = iso,
};
//http = https://aips.um.si/PredmetiBP5/Main.asp?Mode=prg&Zavod=77&Jezik=&Nac=1&Nivo=P&Prg=1571&Let=1
string http = formatLetnikLink(l.Attributes["onclick"].Value).ToString();           
var htmlProgDoc = web.Load(http);
string s = htmlProgDoc.ParsedText;

htmlprogDoc.ParsedText 正确包含所有行 应该在最后 table (我有这个用于调试,以防万一手表 window 坏了或其他什么...idk...)

我试图先把网站上table的所有table都搞定。并意识到有 6

<table></table>

上面有标签,即使你肉眼只能看到一个。调试了几个小时后,我意识到最后一个主要 table,是最后一个

<table>

在 dom 树中,并且解析器完全解析所有

<tr>

table 拥有的标签。这就是问题所在,我需要所有的 tr 标签。

var tables = htmlProgDoc.DocumentNode.SelectNodes("//table");

有6次

<table></table>

tags,正如预期的那样,并且他们中的每个人都被完全解析,包括他们所有的行和列,除了最后一个,在最后一个它只解析前两行然后解析器似乎附加了一个

 </table> 

我自己也尝试使用从 firefox 复制的直接 xpath 选择器: “/html/body/div/center[2]/font/font/font/table”,而不是“//table” 找到了正确的 table,但是 table 也只包含前两行

var theTableINeed = tables.Last();
//contains the correct table which I need, but with only the first two rows

该页面上的 Html 格式不正确。一种可能的解决方法是剥离最后 table 的代码并将其解析为文档。

var client = new WebClient();
string html = client.DownloadString(url);
int lastTableOpen = html.LastIndexOf("<table");
int lastTableClose = html.LastIndexOf("</table");
string lastTable = html.Substring(lastTableOpen, lastTableClose - lastTableOpen + 8);

然后使用HtmlAgilityPack:

var table = new HtmlDocument();
table.LoadHtml(lastTable);
foreach (var row in table.DocumentNode.SelectNodes("//table//tr"))
{
    Console.WriteLine(row.ToString());
}

但是不知道是不是table本身有问题