a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

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

2020-01-24 03:10:11
字體:
供稿:網(wǎ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:///'](http://////|//.///|///)?//w+(//.//w+)*(/////w+(//.//w+)?)*(///|//?//w*=//w*(&//w*=//w*)*)?[///"http:///']";
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的的方法,大家有什么其他好的方法還望指教。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 日本亚洲欧美 | 天堂欧美城网站 | 99久久综合网 | 色成人免费网站 | 日韩高清国产一区在线 | 日韩视频在线一区二区 | 日韩中文字幕av | 欧洲一区二区三区免费视频 | 久久免费视频观看 | 免费观看一区二区三区毛片 | 久久夜夜爽 | 日本一区二区高清视频 | 色婷婷一区二区三区 | 欧美精品一区二区三区一线天视频 | 亚洲在线视频 | 国产精品一区在线 | 日韩一区二区视频 | 欧美成人精品一区二区男人小说 | 国产丝袜人妖ts黄檬 | 久久久久久国产免费视网址 | 绯色av一区二区三区在线高清 | 欧美一区二区三区免费电影 | 裸体喂奶一级裸片 | 国产高清毛片 | 午夜激情在线 | 欧美精品xxx| 成人一区二区三区 | 中文在线一区二区 | 九九九九精品九九九九 | 亚洲 欧美日韩 国产 中文 | 国产精品人成在线播放新网站 | 欧美久草 | 91精品麻豆日日躁夜夜躁 | 欧美日韩中文字幕 | 日韩电影一区 | 禁果av一区二区三区 | 国产一区二区三区在线免费观看 | 国产精品99精品久久免费 | 成人不卡| 国产91在线网站 | 一本色道久久综合亚洲精品不卡 |