可能重复:
在C#中解析html的最佳方法是什么?
有没有办法解析HTML或将HTML转换为XML,以便我轻松地从网站中提取信息?
我正在使用C#.
谢谢,
您可以使用COM对象Microsoft HTML Object Library加载HTML,然后使用它的对象模型进行导航.一个例子如下所示:
string html;
WebClient webClient = new WebClient();
using (Stream stream = webClient.OpenRead(new Uri("http://www.google.com")))
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
IHTMLDocument2 doc = (IHTMLDocument2)new HTMLDocument();
doc.write(html);
foreach (IHTMLElement el in doc.all)
Console.WriteLine(el.tagName);
Run Code Online (Sandbox Code Playgroud)