使用HTML敏捷数据包从网站上获取几个表并将其添加到列表



我正在研究一个小程序,该程序正在从网站获取表信息,以后将按DateTime对此信息进行排序。

我最大的问题是,在获得网站并将其制成字符串的功能之后,我找不到将信息输入列表的方法。我不断遇到nullpointer错误。

我尝试了:

WebClient webClient = new WebClient();
string page = webClient.DownloadString("http://www.mufap.com.pk/payout-
report.php?tab=01");
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);
List<List<string>> table = 
doc.DocumentNode.SelectSingleNode("//table[@class='1']")
        .Descendants("tr")
        .Skip(1)
        .Where(tr=>tr.Elements("td").Count()>1)
        .Select(tr => tr.Elements("td")
        .Select(td => td.InnerText.Trim()).ToList())
        .ToList();

但是由于某种原因,我一直遇到此错误:

An unhandled exception of type 'System.NullReferenceException' occurred in WebGetter.exe

我认为这与我选择的类有关,尽管表的类名为" 1",因此应该具有正确的参考。当我使用

我不断得到这个:

System.Collections.Generic.List1[System.Collections.Generic.List1[System.String]]

如果您可以将我指向正确的方向,那将很好。

如果您需要从HTML选择第一个Table元素,则只需使用SelectSingleNode方法即可。它返回查询的第一个节点,您的查询应该看起来像

doc.DocumentNode.SelectSingleNode("//table")

在您的代码中,您还将查询添加到class,而没有现有值。正如我理解上面的评论时,您认为[class='1']将返回第一个Table元素,这是错误的,它将返回Table元素的class属性值是1

最新更新