看板 C_Sharp 關於我們 聯絡資訊
目前參考此Blog:http://blog.xuite.net/metafun/life/244633345 嘗試利用 HtmlAgilityPack 去解析網頁的HTML標籤 目標網頁是專門用來觀看各地風速預報的「WindGuru」:http://www.windguru.cz/int/ 經由FireFox的相關XPath插件得知整張表格的第一個Col之XPath為: /html[1]/body[1]/div[3]/div[2]/table[1]/tr[3]/td[1]/div[1]/table[1] 在模仿前述網頁進行實作時,發現在程式讀取到第一個table標籤時,會出現NullReferenceException /html[1]/body[1]/div[3]/div[2]/table[1]/tr[3]/td[1]/div[1]/table[1] 反覆看了好幾次路徑及上網爬了資料,還是無法解決 以下是我的未完成程式碼片段,麻煩各位前輩給點提示...謝謝大家 static void Main(string[] args) { string targetUrl = "http://www.windguru.cz/int/"; WebClient client = new WebClient(); MemoryStream ms = new MemoryStream(client.DownloadData(targetUrl)); // 使用預設編碼讀入 HTML HtmlDocument doc = new HtmlDocument(); doc.Load(ms, Encoding.Default); // 裝載第一層查詢結果 HtmlDocument docStockContext = new HtmlDocument(); string targetNode = "/html[1]/body[1]/div[3]/div[2]/table[1]/tr[3]/td[1]/div[1]/table[1]"; docStockContext.LoadHtml(doc.DocumentNode.SelectSingleNode(targetNode).InnerHtml); Console.WriteLine("OK..."); Console.ReadLine(); } -- Sent form my Apple Watch. -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.127.251.185 ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1458565578.A.4D0.html
Litfal: 你是要抓哪個東西?為什麼不用id... 03/21 22:10
Litfal: 而且實際算你抓的div[3]/div[2]裡面的確沒有table阿 03/21 22:11