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;
}

 

posted @   秋香姑娘请你不要紧张  阅读(12061)  评论(2编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
点击右上角即可分享
微信分享提示