2013-03-23 100 views
1

我對C#和特定的HtmlAgilityPack非常陌生,而且我無法從網站獲取信息。例如,我想從該網站的表中獲取圖像的URL: Serebii如何從網站的表格中找到並提取信息?

從網站我試圖尋找並提取以下內容:

Link to picture

string s = "http://www.serebii.net/pokedex-rs/005.shtml"; 

     HtmlWeb hw = new HtmlWeb(); 
     HtmlAgilityPack.HtmlDocument doc = hw.Load(s); 

     //HtmlNodeCollection items = doc.DocumentNode.SelectNodes("//a[@class='question-hyperlink']"); 
     HtmlNodeCollection items = doc.DocumentNode.SelectNodes("//table//tr//td//div//table//tbody//tr//td//img"); 
     foreach (HtmlNode item in items) 
     { 
      Console.WriteLine(item.OuterHtml); 
      MessageBox.Show(item.OuterHtml); 
     } 

     Console.ReadLine(); 

我相當一定我沒有球,任何幫助將不勝感激。

+0

當你說提取時,你到底需要做什麼?該表顯然只是包含圖像的路徑,那麼您是否只想獲取圖像的URL或實際下載並保存每個圖像的本地副本? – 2013-03-23 02:57:20

回答

1

你只能希望開發者不喜歡經常更新源代碼。

var item = doc.DocumentNode.SelectSingleNode("//table//tr//tr//td//div//tr//img"); 
string imageSrc = item.GetAttributeValue("src", ""); 
Console.WriteLine(imageSrc); 
+0

非常感謝,請問「[0]」代表什麼?我已經玩了一下,但我不太明白。 – 2013-03-23 04:24:29

+0

[0]返回數組中的第一個項目,因爲有多個圖像匹配該格式。 – coolmine 2013-03-23 04:25:53

+0

啊,我明白了。謝謝 – 2013-03-23 04:40:43