c# - 给定一个字符串中的网站 HTML,如何提取标签元素?

标签 c# asp.net

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.home.com");
myRequest.Method = "GET";
WebResponse myResponse = myRequest.GetResponse();
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), 
                                   System.Text.Encoding.UTF8);
string result = sr.ReadToEnd();
sr.Close();
myResponse.Close();

该字符串包含该网页的整个 html,现在我想从该字符串中提取 html 标签。

我该怎么做?

最佳答案

Html Agility Pack让解析 HTML 内容变得轻而易举。 你可以看例子here .

HtmlDocument doc = new HtmlDocument();
 doc.Load("file.htm");
 foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
 {
    HtmlAttribute att = link["href"];
    att.Value = FixLink(att);
 }
 doc.Save("file.htm");

关于c# - 给定一个字符串中的网站 HTML,如何提取标签元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14031095/

相关文章:

c# - TextBlock.Text 更新 ViewModel 和 CodeBehind WPF

c# - 离开页面时调用 Page_Load

c# - 不同选定项目的 WPF 组合框背景

asp.net - Blazor Webassembly 身份验证非常慢

c# - 使用 EF 的数据库优先中的连接字符串无法正常工作

C#:等效于 python try/catch/else block

javascript - 通过 mvc 中的 ajax actionlink 获取成功的响应 header

c# - LINQ select 内部的方法调用(效率)

c# - asp : no file is created when logging a message 中的 Log4Net

c# - 为什么 string.IsNullOrEmpty 比比较快?