C# HtmlDocument和HtmlNode的使用以及節點的模糊查詢


C#HtmlAgilityPack.HtmlDocument和HtmlAgilityPack.HtmlNode的使用

  HtmlAgilityPack.HtmlDocument response = null;
            //HtmlAgilityPack.HtmlNode類和HtmlAgilityPack.HtmlDocument類的使用!
            HtmlAgilityPack.HtmlNode responseNew = null;

            HtmlDocument doc = new HtmlDocument();
 wc.Encoding = Encoding.UTF8;
                          string html= wc.DownloadString(url);
                          doc.LoadHtml(html);

                          responseNew = doc.DocumentNode.SelectSingleNode("/html/body");     //根據XPath查找節點,跟XmlNode差不多

                    HtmlNodeCollection categoryNodeList = responseNew.SelectNodes("div[3]/div[1]/div[1]/div[1]/ul[1]/li");
                    foreach (HtmlNode item in categoryNodeList)
                    //foreach (HtmlNode item in ulS2.ChildNodes)
                    {
                        var xpath = item.XPath;
                        if (k % 2 != 0)
                        {
                            number = number + 1;
                            xpath = xpath.Replace("/#text[" + number + "]", "/li");//替換字符串
                        }
                      
                        k=k+1;
                        string titleName, infourl, company, city, date, salary, salary_em, source;
                        titleName = item.SelectSingleNode(xpath + "/div/div/span/a").InnerText;//
                        infourl = item.SelectSingleNode(xpath + "/div/div/span/a").Attributes["href"].Value; //url
}

XML節點的模糊查詢:contains(@屬性,'模糊查詢的值')

 ulS = response.DocumentNode.SelectNodes("//*[@id='resultList']/div[contains(@class,'el')]");

XML節點的等值查詢

ulS = response.DocumentNode.SelectNodes("//*[@id='resultList']/div[@class='el']");
  for (int i = 2; i < ulS.Count; i++)
                    {
                        var item = ulS[i];
                        var xpath = item.XPath;
                        string titleName;
                        titleName = item.SelectSingleNode(xpath + "/p/span/a").InnerText;
}

 


免責聲明!

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



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