使用HtmlAgilityPack

时间:2016-01-16 12:31:50

标签: c# html .net html-parsing html-agility-pack

我正在尝试使用HtmlAgilityPack从div中获取值。 我的htmlcode是这样的: enter image description here

我需要在news_content_container div类中获取值,因为您已经选择了图片,所以我使用了这段代码:

     var response1 = await http.GetByteArrayAsync("http://www.nsfund.ir/news?"+link);
                String source1 = Encoding.GetEncoding("utf-8").GetString(response1, 0, response1.Length - 1);
                source1 = WebUtility.HtmlDecode(source1);
                HtmlDocument resultat1 = new HtmlDocument();
                resultat1.LoadHtml(source1);
               var val = resultat1.DocumentNode.Descendants().Where
  (x => (x.Name == "div" && x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("news_content_container"))).ToList().First().InnerText;;

但结果是空的。

1 个答案:

答案 0 :(得分:1)

试试这个

var response1 = await http.GetByteArrayAsync("http://www.nsfund.ir/news?"+link);
                String source1 = Encoding.GetEncoding("utf-8").GetString(response1, 0, response1.Length - 1);
                source1 = WebUtility.HtmlDecode(source1);
                HtmlDocument resultat1 = new HtmlDocument();
                resultat1.LoadHtml(source1);
               var val = resultat1.DocumentNode.SelectSingleNode("//div[@class='news_content_container']").InnerText;