2017-05-06 24 views
0

我正在從一個網站獲取表信息的小程序,稍後將按DateTime對這些信息進行排序。使用HTML敏捷包從網站獲取多個表並將它們添加到列表

我最大的問題是,我的函數獲取網站並將其變成一個字符串後,我找不到一種方法將信息獲取到列表中。我不斷收到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]]

如果你可以點我到正確的方向,這將是很好的。

+0

忘了提,這是我使用的鏈接: https://www.athletic.net/TrackAndField/Division/Top.aspx?DivID=81830 –

+0

在給定的網址中沒有任何含'1'類的表 –

+0

@RubenVardanyan那麼我該如何獲得第一個表?我對此很新穎:s –

回答

0

如果您需要從HTML中選擇第一個Table元素,您只需使用SelectSingleNode方法即可。它返回的第一個節點爲您查詢,您的查詢應該是這樣的

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

在你的代碼也添加查詢class與不存在的價值。據我瞭解,形成上述您的意見,您認爲[class='1']將返回第一個Table元素,這是錯誤的,它會返回Table元件,它是class屬性值是1