怎么样:使用HTML Agility Pack
Htmldocument doc = new Htmldocument();doc.LoadHtml(@"<html><body><p><table id=""foo""><tr><th>hello</th></tr><tr><td>world</td></tr></table></body></html>");foreach (HtmlNode table in doc.documentNode.SelectNodes("//table")) { Console.WriteLine("Found: " + table.Id); foreach (HtmlNode row in table.SelectNodes("tr")) { Console.WriteLine("row"); foreach (HtmlNode cell in row.SelectNodes("th|td")) { Console.WriteLine("cell: " + cell.InnerText); } }}
请注意,如果需要,可以使用LINQ-to-Objects使其更漂亮:
var query = from table in doc.documentNode.SelectNodes("//table").Cast<HtmlNode>() from row in table.SelectNodes("tr").Cast<HtmlNode>() from cell in row.SelectNodes("th|td").Cast<HtmlNode>() select new {Table = table.Id, CellText = cell.InnerText};foreach(var cell in query) { Console.WriteLine("{0}: {1}", cell.Table, cell.CellText);}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)