C#中使用HtmlAgilityPack解析html

时间:2016-04-05 14:23:25 阅读:729次
C#中使用HtmlAgilityPack解析html

//根据Xpath获取单个节点

HtmlAgilityPack.HtmlNode htmlnode = doc.DocumentNode.SelectSingleNode("/html/body/div[4]/div[1]/div/ul[2]/li[1]/div[2]/p/a");
this.StatusTextBox.AppendText(htmlnode.InnerText);


//根据Xpath获取多个节点

//获取Html页面代码
            MyWeb.HttpHelper httpHelper = new MyWeb.HttpHelper();
            MyWeb.HttpItem httpItem = new MyWeb.HttpItem();
            MyWeb.HttpResult httpResult = new MyWeb.HttpResult();

            httpItem.URL = "http://wenda.henan28.com";
            httpResult = httpHelper.GetHtml(httpItem);
            if (!String.IsNullOrEmpty(httpResult.Html))
            {
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                //第二步加载html文档
                doc.LoadHtml(httpResult.Html);
                HtmlAgilityPack.HtmlNodeCollection collection = doc.DocumentNode.SelectNodes("/html/body/div[4]/div[1]/div/ul[2]/li/div[2]/p/a");
                if (collection != null)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (HtmlAgilityPack.HtmlNode item in collection)
                    {
                        sb.Append(string.Format("{0}:{1}\r\n", item.InnerText, item.Attributes["href"].Value));
                    }
                    this.StatusTextBox.AppendText(sb.ToString());
                }
            }


引用:HtmlAgilityPack

扫描二维码关注程序员爱笔记,接收更多资讯

评论

快速评论