將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("//@*");//遍歷所有標簽

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM