我正在研究一个小程序,该程序正在从网站获取表信息,以后将按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