【问题标题】:get a data with HTMLagilitypack from a span从跨度获取带有 HTMLagilitypack 的数据
【发布时间】:2013-12-17 18:15:50
【问题描述】:

我想使用 HtmlAgilityPack 从网站中获取一些数据。 数据存储在属性为class="addresscolor__" 的对象中。但问题是这个名字不止一个类。我想知道有没有办法从具有该值的特定类中获取这些数据,使用“Start with”或“Contained..”或类似的东西?

HTML 代码:

<p class="addresscolor__">
    <span>Phone:</span>
    33875362 ، 22356341
    <br />
</p>

<p class="addresscolor__">
    <span>mobile:</span>
    09125231827
    <br />
</p>

我使用这个代码:

HtmlNodeCollection nodes2 = doc.DocumentNode.SelectNodes("//p[@class='addresscolor__']");

另一件事是,这个类包含跨度标记,使用这些代码我可以捕获整个&lt;p&gt; 标记值,但我只想要一个“移动”跨度标记中的数字。

【问题讨论】:

    标签: c# html asp.net xpath html-agility-pack


    【解决方案1】:

    为了找到这个段落:

    //p[@class='addresscolor__' and contains(span, 'mobile:')]
    

    用于限制其文本节点(电话号码):

    //p[@class='addresscolor__' and contains(span, 'mobile:')]/text()
    

    为了进一步去除空白:

    normalize-space(//p[@class='addresscolor__' and contains(span, 'mobile:')]/text())
    

    对于匹配的 HTML 类,还可以考虑 a node can have multiple classes and how to deal with that

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多