C#如何解析HTML
//the sibling nodes if (siblingRequired) {
INode sibling = htmlNode.NextSibling; while (sibling != null) {
this.RecursionHtmlNode(treeNode, sibling, false); sibling = sibling.NextSibling; } } } private void AddUrl() { CBUrl.Items.Add(""); CBUrl.Items.Add(""); CBUrl.Items.Add(""); }
收集整理
在搜索引擎的开发中,我们需要对网页的 Html 内容进行检索,难免的就需要对 Html 进行解析。拆分每一个节点并且 获取节点间的内容。此文介绍两种 C#解析 Html 的方法。
第一种方法:
用 .WebClient 下载 Web Page 存到本地文件或者 String 中,用正则表达式来分析。这个方法可以用在 Web Crawler 等需要分析很多 Web Page 的应用中。 估计这也是大家最直接,最容易想到的一个方法。 转自网上的一个实例:所有的 href 都抽取出来:
namespace HTMLParser {
public partial class Form1 : Form {
public Form1() l(); }
private void btnParser_Click(object sender, EventArgs e) {
System.Collections.IEnumerator enu = matches.GetEnumerator(); while (enu.MoveNext() && enu.Current != null) {
Match match = (Match)(enu.Current); Console.Write(match.Value + "\r\n"); } } } }
nodeString = nodeString + " { href=\"" + tag.Attributes["HREF"].ToString() + "\ " }";
} }
current = new TreeNode(nodeString); treeNode.Nodes.Add(current);
#region 分析网页 html 节点
收集整理
收集整理
Lexer lexer = new Lexer(this.txtHtmlWhole.Text); Parser parser = new Parser(lexer); NodeList htmlNodes = parser.Parse(null); this.treeView1.Nodes.Clear(); this.treeView1.Nodes.Add("root"); TreeNode treeRoot = this.treeView1.Nodes[0]; for (int i = 0; i < htmlNodes.Count; i++) {
收集整理
收集整理
} }
//获取节点间的内容 if (htmlNode.Children != null && htmlNode.Children.Count > 0) {
this.RecursionHtmlNode(current, htmlNode.FirstChild, true); content = new TreeNode(htmlNode.FirstChild.GetText()); treeNode.Nodes.Add(content); }
using System; using ; using System.Text; using System.Text.RegularExpressions; namespace HttpGet {
class Class1 {
[STAThread] static void Main(string[] args) {
.WebClient client = new WebClient(); byte[] page = client.DownloadData(""); string content = System.Text.Encoding.UTF8.GetString(page); string regex = "href=[\\\"\\\'](http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w* =\\w*(&\\w*=\\w*)*)?[\\\"\\\']"; Regex re = new Regex(regex); MatchCollection matches = re.Matches(content);
#region 获得网页的 html try {
txtHtmlWhole.Text = ""; string url = CBUrl.SelectedItem.ToString().Trim(); .WebClient aWebClient = new .WebClient(); aWebClient.Encoding = System.Text.Encoding.Default; string html = aWebClient.DownloadString(url); txtHtmlWhole.Text = html; } catch (Exception ex) { MessageBox.Show(ex.Message); } #endregion
this.RecursionHtmlNode(treeRoot, htmlNodes[i], false); }
#endregion
}
private void RecursionHtmlNode(TreeNode treeNode, INode htmlNode, bool siblingRequire d)
收集整理
收集整理
using System; using System.Collections.Generic; using ponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Winista.Text.HtmlParser; using Winista.Text.HtmlParser.Lex; using Winista.Text.HtmlParser.Util; using Winista.Text.HtmlParser.Tags; using Winista.Text.HtmlParser.Filters;
一些爬虫的 HTML 解析中也是用的类似的方法。
第二种方法:
利用 解析 Html。这是.NET 平台下解析文的帮助文档。找不到的留下邮箱。 个人认为这是.net 平台下解析 html 不错的解决方案,基本上能够满足我们对 html 的解析工作。 自己做了个实例:
string nodeString = tag.TagName; if (tag.Attributes != null && tag.Attributes.Count > 0) {
if (tag.Attributes["ID"] != null) {
nodeString = nodeString + " { id=\"" + tag.Attributes["ID"].ToString() + "\" }"; } if (tag.Attributes["HREF"] != null) {
} }
收集整理
{ if (htmlNode == null || treeNode == null) return;
TreeNode current = treeNode; TreeNode content ; //current node if (htmlNode is ITag) {
ITag tag = (htmlNode as ITag); if (!tag.IsEndTag()) {