【问题标题】:how do i get the text from a nested <p> tag in an external html using html agility pack?如何使用 html 敏捷包从外部 html 中的嵌套 <p> 标记中获取文本?
【发布时间】:2012-04-27 06:46:48
【问题描述】:

我正在尝试从外部站点获取一些文本。我要获取的文本嵌套在段落标签中。 div 有一个类值

html代码sn-p:

<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 应该是什么是错误的?

【问题讨论】:

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


    【解决方案1】:

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

    细分:

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

    【讨论】:

      猜你喜欢
      • 2020-01-19
      • 2012-04-17
      • 2013-02-28
      • 2017-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多