1. 程式人生 > >ASP.NET MVC - 處理Html數據

ASP.NET MVC - 處理Html數據

頁面元素 祖先 tor client con 標簽 調用 html font

HtmlAgilityPack

使用HtmlAgilityPack可以以面向對象的方式通過查找Html節點來獲取頁面元素。參考:http://html-agility-pack.net

HtmlDocument類

//方法
LoadHtml(string content);
//加載Html數據

CreateNode(string html)
//創建一個HtmlNode

CreateAttribute(string attriName, string attriVal)
//創建一個屬性

CreateTextNode(string text)
//創建文本

//其它參考:http://html-agility-pack.net/utilities和http://html-agility-pack.net/writer


//屬性
DocumentNode
//得到根節點,返回一個HtmlNode

HtmlNode類 //方法
SelectNodes(string xPath)
//獲取xPath指定的節點集合

SelectSingleNode(string xPath)
//獲取xPath指定的節點集合中的第一個節點

SetAttributeValue(HtmlAttribute attri | string attriName, string attriVal)
//修改當前節點的屬性

PrependChild(HtmlNode node)
//在當前節點中的開始處插入新的子節點

AppendChild(HtmlNode
node)
//在當前節點中的末尾處插入新的子節點

PrependChildren(HtmlNode node)
//在當前節點中的開始處的子節點的開始處插入後代節點

AppendChildren(HtmlNode node)
//在當前節點中的末尾處的子節點的末尾處插入後代節點

InsertAfter(HtmlNode node)
//在當前節點後面插入新節點,類似的有insertBefore

Remove()
//移除自身

RemoveAll()
//移除自身包含的所有節點

RemoveAllChildren()
//移除自身包含的所有節點

RemoveChild(HtmlNode oldChild | HtmlNode oldChild, bool
keepGrandChildren)
//keepGrandChildren:是否深度移除後代節點
//移除自身包含的由參數指定的節點

ReplaceChild(HtmlNode newChild, HtmlNode oldChild);
//在當前節點中,用newChild替換oldChild

Clone()
//創建副本

CloneNode(bool deep | string name | string name, bool deep)
//deep:是否深度克隆,如果是false,則只克隆節點自身
//name:克隆的同時修改節點的標簽名稱,比如克隆H1的內容同時改掉結果節點的名稱為H2

CopyFrom(HtmlNode node | HtmlNode node, bool deep)
//deep:是否深度拷貝,如果是false,則只拷貝節點自身
//從參數指定的節點處復制其包含的所有節點

Element()
//在當前節點中根據參數指定的名稱獲取一個子節點,返回單個HtmlNode

Elements()
//在當前節點中根據參數指定的名稱獲取子節點集合,返回IEnumerable<HtmlNode>集合

Ancestors(string name)
//獲取當前節點的由參數指定的名稱的祖先節點,如果參數為空則獲取當前節點的所有祖先節點的集合,類似的有AncestorsAndSelf()、AncestorsAndSelf(string name)

Descendants(string name)
//獲取當前節點的由參數指定的名稱的後代節點,如果參數為空則獲取當前節點的所有後代節點的集合,類似的有DescendantsAndSelf()、DescendantsAndSelf(string name)

DescendantNodes()
//獲取所有後代節點,類似的有DescendantNodesAndSelf()

//屬性
InnerHtml
//設置或獲取當前節點所包含的元素節點

InnerText
//設置或獲取當前節點所包含的文本

OuterHtml
//獲取當前節點所包含的所有代碼

ParentNode
//獲取當前節點的父節點

ChildNodes
//獲取所有子節點,類似的有FirstChild、LastChild、NextSibling、ParentNode

HtmlAttribute類

//方法
Add(HtmlAttribute attri | string attriName, string attriVal)
//添加一個屬性,類似的有Append()

Remove(string attriName)
//此方法需要屬性集合調用,移除節點的由參數指定的屬性,如果參數為空則移除所有屬性,類似的有RemoveAll()、RemoveAt(int index)

例子:

private void Test()
{
HtmlDocument doc = new HtmlDocument();
//三種方式創建Html對象

//第一種:
//System.IO.StreamReader reader = System.IO.File.OpenText(url);
//doc.Load(reader);

//第二種:
//doc.LoadHtml(url); 如果使用Load(url)會出現提示文檔包含非法字符

//第三種:
//string htmlText = "<div>xxx</div>";
//doc.LoadHtml(htmlText);

string url = "http://www.weather.com.cn/weathern/101040100.shtml";
WebClient wc = new WebClient();
wc.Encoding = Encoding.GetEncoding("utf-8");
string content = wc.DownloadString(url);

doc.LoadHtml(content);
HtmlNode root = doc.DocumentNode; //得到根節點
var div= root.SelectNodes("//div").Where(d => d.InnerText.Contains("sam")).SingleOrDefault();
}

ASP.NET MVC - 處理Html數據