将html转化成dom对象 然后进行分析

 

private delegate string DelegateGetDocument();
private string GetDocument()
{
return webCollection.Document.Body.InnerHtml;
}

*****************************************

下面使用的是webbrowser抓取数据

*****************************************

HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();//using HtmlAgilityPack;
string doc = "";
doc = this.Invoke(new DelegateGetDocument(GetDocument)).ToString();
document.LoadHtml(doc);
HtmlNode root = document.DocumentNode;//root根节点

分析举例:

HtmlNodeCollection TargetNodes = root.SelectNodes("//h3[@class='summary']");//h3 class="summary"

HtmlNode priceLabel = root.SelectSingleNode("//strong[@id='J_StrPrice']");

HtmlNode TargetNode = TargetNodes[ii].SelectSingleNode("./a");

HtmlNode DivNode = root.SelectSingleNode("//div[@id='thread_theme_3']");
HtmlNodeCollection TagretNodes = DivNode.SelectNodes("//@*");//遍历所有标签

 

你可能感兴趣的:(DOM对象)