如何使用html敏捷包从外部html中的嵌套<p>标签中获取文本?</p>

时间:2012-04-15 20:11:39

标签: c# html xpath html-agility-pack

我正在尝试从外部网站获取一些文本。我想要获得的文本嵌套在段落标记中。 div具有类值

html代码段:

<div class="discription"><p>this is the text I want to grab</p></div>

当前的c#代码:

public String getDiscription(string url)
{
    var web = new HtmlWeb();
    var doc = web.Load(url);


    var nodes = doc.DocumentNode.SelectNodes("//div[@class='discription']");

    if (nodes != null)
    {
        foreach (var node in nodes)
        {
            string Description = node.InnerHtml;
            return Description;
        }
    } else
      {
       string error = "could not find text";
       return error;
      }
}

我不理解的是xpath //div[@class='discription']的语法我知道xpath应该是什么错误?

1 个答案:

答案 0 :(得分:0)

使用//div[@class='discription']/p

故障:

//div                    - All div elements
[@class='discription']   - With a class attribute whose value is discription
/p                       - Select the child p elements