欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#下解析HTML的兩種方法介紹

 更新時間:2013年09月05日 08:39:21   作者:  
用System.Net.WebClient下載Web Page存到本地文件或者String中,用正則表達式來分析。這個方法可以用在Web Crawler等需要分析很多Web Page的應(yīng)用中

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

C#解析Html的第一種方法:
用System.Net.WebClient下載Web Page存到本地文件或者String中,用正則表達式來分析。這個方法可以用在Web Crawler等需要分析很多Web Page的應(yīng)用中。
估計這也是大家最直接,最容易想到的一個方法。
轉(zhuǎn)自網(wǎng)上的一個實例:所有的href都抽取出來:

復(fù)制代碼 代碼如下:

using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace HttpGet
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
System.Net.WebClient client = new WebClient();
byte[] page = client.DownloadData("http://www.google.com");
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);

System.Collections.IEnumerator enu = matches.GetEnumerator();
while (enu.MoveNext() && enu.Current != null)
{
Match match = (Match)(enu.Current);
Console.Write(match.Value + "\r\n");
}
}
}


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

個人認為這是.net平臺下解析html不錯的解決方案,基本上能夠滿足我們對html的解析工作。
自己做了個實例:
復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
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;


namespace HTMLParser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
AddUrl();
}

private void btnParser_Click(object sender, EventArgs e)
{
#region 獲得網(wǎng)頁的html
try 
{

txtHtmlWhole.Text = "";
string url = CBUrl.SelectedItem.ToString().Trim();
System.Net.WebClient aWebClient = new System.Net.WebClient();
aWebClient.Encoding = System.Text.Encoding.Default;
string html = aWebClient.DownloadString(url);
txtHtmlWhole.Text = html;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
#endregion

#region 分析網(wǎng)頁html節(jié)點
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++)
{
this.RecursionHtmlNode(treeRoot, htmlNodes[i], false);
}

#endregion

}

private void RecursionHtmlNode(TreeNode treeNode, INode htmlNode, bool siblingRequired)
{
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())
{
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)
{
nodeString = nodeString + " { href=\"" + tag.Attributes["HREF"].ToString() + "\" }";
}
}

current = new TreeNode(nodeString);
treeNode.Nodes.Add(current);
}
}
//獲取節(jié)點間的內(nè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);
}
//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("http://www.hao123.com");
CBUrl.Items.Add("http://www.sina.com");
CBUrl.Items.Add("http://www.heuet.edu.cn");
}
}


運行效果:

運行效果 

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

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

相關(guān)文章

  • 詳解C#中IAsyncDisposable接口的使用

    詳解C#中IAsyncDisposable接口的使用

    在.NET Core 3.0的版本更新中,官方我們帶來了一個新的接口 IAsyncDisposable,下面小編就來和大家聊聊它的簡單使用吧,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-07-07
  • C#?使用Aspose.Cells?導(dǎo)出Excel的步驟及問題記錄

    C#?使用Aspose.Cells?導(dǎo)出Excel的步驟及問題記錄

    Aspose.Cells是一款功能強大的Excel文檔處理和轉(zhuǎn)換控件,開發(fā)人員和客戶電腦無需安裝Microsoft Excel也能在應(yīng)用程序中實現(xiàn)類似Excel的強大數(shù)據(jù)管理功能,對C#?使用Aspose.Cells?導(dǎo)出Excel的步驟及問題記錄感興趣的朋友一起看看吧
    2022-01-01
  • C# List中FindAll用法的一些簡單示例

    C# List中FindAll用法的一些簡單示例

    本篇文章只要是對C# List中FindAll用法的一些簡單示例進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • C#異步編程詳解

    C#異步編程詳解

    本文主要介紹異步編程中Task、Async和Await的基礎(chǔ)知識。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • C#基礎(chǔ)繼承和多態(tài)詳解

    C#基礎(chǔ)繼承和多態(tài)詳解

    C#基礎(chǔ)繼承和多態(tài)詳解,需要的朋友可以參考一下
    2013-03-03
  • c# SqlDataAdapter中的Fill是怎么實現(xiàn)的

    c# SqlDataAdapter中的Fill是怎么實現(xiàn)的

    這篇文章主要介紹了c# SqlDataAdapter中的Fill是怎么實現(xiàn)的,文中講解非常細致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • Winform窗體中打開PDF文件的三種方式

    Winform窗體中打開PDF文件的三種方式

    這篇文章介紹了Winform窗體中打開PDF文件的三種方式,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • 淺談C#中的Infinity和NaN

    淺談C#中的Infinity和NaN

    下面小編就為大家?guī)硪黄獪\談C#中的Infinity和NaN。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • C#處理文本文件TXT實例詳解

    C#處理文本文件TXT實例詳解

    這篇文章主要介紹了C#處理文本文件TXT的方法,以實例形式詳細分析了txt文本文件的讀取、修改及打印等功能的實現(xiàn)技巧,需要的朋友可以參考下
    2015-02-02
  • C#中richtextbox使用方法詳解

    C#中richtextbox使用方法詳解

    這篇文章主要介紹了C#中richtextbox使用方法,分析較為詳盡,需要的朋友可以參考下
    2014-07-07

最新評論