在搜尋引擎的開發中,我們需要對html進行解析。本文介紹c#解析html的兩種方法。
ad:
在搜尋引擎的開發中,我們需要對網頁的html內容進行檢索,難免的就需要對html進行解析。拆分每乙個節點並且獲取節點間的內容。此文介紹兩種c#解析html的方法。
c#解析html的第一種方法:
估計這也是大家最直接,最容易想到的乙個方法。
**網上的乙個例項:所有的href都抽取出來:
一些爬蟲的html解析中也是用的類似的方法。
c#解析html的第二種方法:
個人認為這是.net平台下解析html不錯的解決方案,基本上能夠滿足我們對html的解析工作。
自己做了個例項:
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
private
void btnparser_click(object sender, eventargs e)
catch (exception ex)
#endregion
#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
#endregion
} private
void recursionhtmlnode(treenode treenode, inode htmlnode, bool siblingrequired)
";
} if (tag.attributes["href"] != null)
";
} }
current = new treenode(nodestring);
treenode.nodes.add(current);
} }
//獲取節點間的內容
if (htmlnode.children != null && htmlnode.children.count > 0)
//the sibling nodes
if (siblingrequired)
} } private
void addurl()
} }
實現取來很容易,結合winista.htmlparser原始碼很快就可以實現想要的效果。
小結:
簡單介紹了兩種c#解析html的的方法,大家有什麼其他好的方法還望指教。
C 介紹C 解析HTML的兩種方法
一些爬蟲的html解析中也是用的類似的方法。c 解析html的第二種方法 個人認為這是.net平台下解析html不錯的解決方案,基本上能夠滿足我們對html的解析工作。自己做了個例項 usingsystem usingsystem.collections.generic usingsystem.co...
詳解Swift model 解析的兩種方法
詳解swift model 解析的兩種方法 1.常規www.cppcns.com解析方法 懶載入宣告乙個ljnewsmodel為資料的陣列 lazy var ljarray ljnewsmodel ljnewsmodel mark 資料獲取和解析 extension newsviewcontroll...
C 讀寫txt檔案的兩種方法介紹
c 讀寫txt檔案的兩種方法介紹 1.新增命名空間 system.io system.text 2.檔案的讀取 1 使用filestream類進行檔案的讀取,並將它轉換成char陣列,然後輸出。byte bydata new byte 100 char chardata new char 1000 ...