我正在做一个非常简单的任务:解析网站,寻找
<tbody>this is what important for me</tbody>`
并返回,但我无法使它工作。当我这样做时:
Regex.Matches(webData, @"<tbody>(.*?)</tbody>")
它没有给我任何结果。然而,这给了我2个结果:
Regex.Matches(webData, @"tbody")
但同样,这个
Regex.Matches(webData, @"tbody(.*?)tbody")
什么也没给我(所以我认为逃避不是问题)。我在this page找到了(.*?)
,我认为它很容易使用,但我无法解决这个问题。
答案 0 :(得分:2)
建议不要使用regex
解析html
regex
用于定期发生的模式。html
与其格式不一致(xhtml
除外)。例如html
文件即使你不要拥有closing tag
!这可能会破坏您的代码。
您可以使用此代码使用HtmlAgilityPack
HtmlDocument doc = new HtmlDocument();
doc.Load(yourStream);
var tbodyList= doc.DocumentNode.SelectNodes("//tBody")
.Select(p => p.InnerText)
.ToList();
tbodyList
包含整个文档中的所有tbody
值!
答案 1 :(得分:2)
要解析网页,请使用像HtmlAgilityPack
这样的真实html解析器string html = "<tbody>this is what important for me</tbody>";
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var text = doc.DocumentNode.Descendants("tbody").First().InnerText;
答案 2 :(得分:0)
我也推荐使用HtmlAgilityPack。
您也可以使用XPath(http://www.w3schools.com/xpath/)
在I4V示例中:
var text = doc.DocumentNode.SelectSingleNode("//tbody").InnerText;