【问题标题】:Xpath get the text from the first instance in each pair of classesXpath 从每对类的第一个实例中获取文本
【发布时间】:2021-05-29 14:19:52
【问题描述】:

我使用以下 xpath 查询来获取文本,但它从两者中获取文本,我只需要从每个组的第一个价格实例中获取它,页面上有几个组,所以我无法限制它。

有没有办法限制它,所以我只用一个类来获得跨度?

$prices = $xpath->query('//span[@class="a-price"]/span');

<div>
    <span class="price">
        text.      <---This is the one I need
    <span>
    <span class="price red">
        text
    <span>
</div>

blah blah blah

<div>
    <span class="price">
        text.      <---This is the one I need
    <span>
    <span class="price red">
        text
    <span>
</div>

blah blah blah 

<div>
    <span class="price">
        text.      <---This is the one I need
    <span>
    <span class="price red">
        text
    <span>
</div>

【问题讨论】:

    标签: php dom xpath


    【解决方案1】:

    使用这个特定的 html,如果您将 xpath 查询更改为

    //div//span[@class="price"][1]/text()
    

    //div//span[@class="price"]/text()[1]
    

    你应该得到预期的输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-29
      • 2020-11-16
      相关资源
      最近更新 更多