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/