免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
介紹C#解析HTML的兩種方法

在搜索引擎的開發(fā)中,我們需要對(duì)網(wǎng)頁的Html內(nèi)容進(jìn)行檢索,難免的就需要對(duì)Html進(jìn)行解析。拆分每一個(gè)節(jié)點(diǎn)并且獲取節(jié)點(diǎn)間的內(nèi)容。此文介紹兩種C#解析Html的方法。

C#解析Html的第一種方法:

用System.Net.WebClient下載Web Page存到本地文件或者String中,用正則表達(dá)式來分析。這個(gè)方法可以用在Web Crawler等需要分析很多Web Page的應(yīng)用中。

估計(jì)這也是大家最直接,最容易想到的一個(gè)方法。

轉(zhuǎn)自網(wǎng)上的一個(gè)實(shí)例:所有的href都抽取出來:

  1. using System;
  2. using System.Net;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. namespace HttpGet
  6. {
  7.     class Class1
  8.     {
  9.         [STAThread]
  10.         static void Main(string[] args)
  11.         {
  12.             System.Net.WebClient client = new WebClient();
  13.             byte[] page = client.DownloadData(“http://www.google.com”);
  14.             string content = System.Text.Encoding.UTF8.GetString(page);
  15.             string regex = ”href=[\"\'](http:\/\/|\.\/|\/)?\w (\.\w )*(\/\w (\.\w )?)*(\/|\?\w*=\w*(&\w*=\w*)*)?[\"\']“;
  16.             Regex re = new Regex(regex);
  17.             MatchCollection matches = re.Matches(content);
  18.             System.Collections.IEnumerator enu = matches.GetEnumerator();
  19.             while (enu.MoveNext() && enu.Current != null)
  20.             {
  21.                 Match match = (Match)(enu.Current);
  22.                 Console.Write(match.Value   ”rn”);
  23.             }
  24.         }
  25.     }
  26. }

一些爬蟲的HTML解析中也是用的類似的方法。

C#解析Html的第二種方法:

利用Winista.Htmlparser.Net 解析Html。這是.NET平臺(tái)下解析Html的開源代碼,網(wǎng)上有源碼下載,百度一下就能搜到,這里就不提供了。并且有英文的幫助文檔。找不到的留下郵箱。

個(gè)人認(rèn)為這是.net平臺(tái)下解析html不錯(cuò)的解決方案,基本上能夠滿足我們對(duì)html的解析工作。

自己做了個(gè)實(shí)例:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Winista.Text.HtmlParser;
  10. using Winista.Text.HtmlParser.Lex;
  11. using Winista.Text.HtmlParser.Util;
  12. using Winista.Text.HtmlParser.Tags;
  13. using Winista.Text.HtmlParser.Filters;
  14. namespace HTMLParser
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.             AddUrl();
  22.         }
  23.         private void btnParser_Click(object sender, EventArgs e)
  24.         {
  25.             #region 獲得網(wǎng)頁的html
  26.             try
  27.             {
  28.                 txtHtmlWhole.Text = ”";
  29.                 string url = CBUrl.SelectedItem.ToString().Trim();
  30.                 System.Net.WebClient aWebClient = new System.Net.WebClient();
  31.                 aWebClient.Encoding = System.Text.Encoding.Default;
  32.                 string html = aWebClient.DownloadString(url);
  33.                 txtHtmlWhole.Text = html;
  34.             }
  35.             catch (Exception ex)
  36.             {
  37.                 MessageBox.Show(ex.Message);
  38.             }
  39.             #endregion
  40.             #region 分析網(wǎng)頁html節(jié)點(diǎn)
  41.             Lexer lexer = new Lexer(this.txtHtmlWhole.Text);
  42.             Parser parser = new Parser(lexer);
  43.             NodeList htmlNodes = parser.Parse(null);
  44.             this.treeView1.Nodes.Clear();
  45.             this.treeView1.Nodes.Add(“root”);
  46.             TreeNode treeRoot = this.treeView1.Nodes[0];
  47.             for (int i = 0; i <  htmlNodes.Count; i )
  48.             {
  49.                 this.RecursionHtmlNode(treeRoot, htmlNodes[i], false);
  50.             }
  51.             #endregion
  52.         }
  53.         private void RecursionHtmlNode(TreeNode treeNode, INode htmlNode, bool siblingRequired)
  54.         {
  55.             if (htmlNode == null || treeNode == null) return;
  56.             TreeNode current = treeNode;
  57.             TreeNode content ;
  58.             //current node
  59.             if (htmlNode is ITag)
  60.             {
  61.                 ITag tag = (htmlNode as ITag);
  62.                 if (!tag.IsEndTag())
  63.                 {
  64.                     string nodeString = tag.TagName;
  65.                     if (tag.Attributes != null && tag.Attributes.Count > 0)
  66.                     {
  67.                         if (tag.Attributes["ID"] != null)
  68.                         {
  69.                             nodeString = nodeString   ” { id=”"   tag.Attributes["ID"].ToString()   ”" }”;
  70.                         }
  71.                         if (tag.Attributes["HREF"] != null)
  72.                         {
  73.                             nodeString = nodeString   ” { href=”"   tag.Attributes["HREF"].ToString()   ”" }”;
  74.                         }
  75.                     }
  76.                     current = new TreeNode(nodeString);
  77.                     treeNode.Nodes.Add(current);
  78.                 }
  79.             }
  80.             //獲取節(jié)點(diǎn)間的內(nèi)容
  81.             if (htmlNode.Children != null && htmlNode.Children.Count > 0)
  82.             {
  83.                 this.RecursionHtmlNode(current, htmlNode.FirstChild, true);
  84.                 content = new TreeNode(htmlNode.FirstChild.GetText());
  85.                 treeNode.Nodes.Add(content);
  86.             }
  87.             //the sibling nodes
  88.             if (siblingRequired)
  89.             {
  90.                 INode sibling = htmlNode.NextSibling;
  91.                 while (sibling != null)
  92.                 {
  93.                     this.RecursionHtmlNode(treeNode, sibling, false);
  94.                     sibling = sibling.NextSibling;
  95.                 }
  96.             }
  97.         }
  98.         private void AddUrl()
  99.         {
  100.             CBUrl.Items.Add(“http://www.hao123.com”);
  101.             CBUrl.Items.Add(“http://www.sina.com”);
  102.             CBUrl.Items.Add(“http://www.heuet.edu.cn”);
  103.         }
  104.     }
  105. }

運(yùn)行效果:

實(shí)現(xiàn)取來很容易,結(jié)合Winista.Htmlparser源碼很快就可以實(shí)現(xiàn)想要的效果。

小結(jié):

簡(jiǎn)單介紹了兩種C#解析Html的的方法,大家有什么其他好的方法還望指教。

Tags:
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
C# html解析器 ,解析HTML的各種工具比較。
HTMLParser
Java資源網(wǎng) 用HTMLParser從HTML中攫取你所需的信息
C# TreeView 樹拖拽
java實(shí)現(xiàn)二叉樹啟遍歷的算法
winfrom權(quán)限設(shè)置樹形菜單
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服