利用htmlagilitypack--抓取网页中的表格数据

例如从http://data.bank.hexun.com/ll/rmbhl.aspx采集每天的汇率

 1             HtmlWeb web = new HtmlWeb();
 2             HtmlAgilityPack.HtmlDocument doc = web.Load(@"http://data.bank.hexun.com/ll/rmbhl.aspx");
 3             doc.LoadHtml(doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/div[5]/div[1]/div[2]/div[1]/div[2]/div[1]/table[1]").InnerHtml);
 4             //取得标题
 5             HtmlNodeCollection nodeHeaders =doc.DocumentNode.SelectNodes("./tr[1]/th");
 6 
 7             //只取得第一行的数值
 8             HtmlNodeCollection nodeValues = doc.DocumentNode.SelectNodes("./tr[2]/td");
 9  
10             //保存资料
11             for (int i = 0; i < nodeHeaders.Count; i++)
12             {
13                Console.WriteLine("Header: {0}, Value: {1}", nodeHeaders[i].InnerText, nodeValues[i].InnerText);
14             }
15 
16             doc = null;
17             web = null;

本人刚学C#欢迎各位前辈们指导一下,感激不尽。

posted on 2013-06-19 00:23  vvangjian  阅读(858)  评论(0编辑  收藏  举报