【问题标题】:XPath scrape data from Table in DIV style, using classes as columns and rows - get adjacent DIV based on DIVXPath 以 DIV 样式从 Table 中抓取数据,使用类作为列和行 - 基于 DIV 获取相邻 DIV
【发布时间】:2017-09-25 03:27:17
【问题描述】:

我试图抓取的页面类似于下面的代码。 根据从一个 DIV 中抓取和验证的文本,我需要从以下 DIV 中抓取值。

我有正确的 XPath 来获取我需要验证的 DIV,但是,我不知道如何从相邻的 DIV 中获取文本?

<h3>TableWithColumnsAndRowsDIVstyle</h3>
<div class="row row-flex rowLine" style="padding: 2px 0;">
    <div class="col-1 vcenter">
        ::before
        <h4 style="white-space: nowrap;">
                        RowValueNameX
        </h4>
    </div>
    <div class="col-2 vcenter text-right">
        ::before
        <h4>
            <b>RowValueX_ThatINeedToExtract</b>
        </h4>
    </div>
    ::after
</div>
<div class="row row-flex rowLine" style="padding: 2px 0;">
        <div class="col-1 vcenter">
            ::before
            <h4 style="white-space: nowrap;">
                            RowValueNameY
            </h4>
        </div>
        <div class="col-2 vcenter text-right">
            ::before
            <h4>
                <b>RowValueY_ThatINeedToExtract</b>
            </h4>
        </div>
        ::after
    </div>
<div class="row row-flex rowLine" style="padding: 2px 0;">
        <div class="col-1 vcenter">
            ::before
            <h4 style="white-space: nowrap;">
                            RowValueNameZ
            </h4>
        </div>
        <div class="col-2 vcenter text-right">
            ::before
            <h4>
                <b>RowValueZ_ThatINeedToExtract</b>
            </h4>
        </div>
        ::after
    </div>

我拥有的 XPath 看起来像这样

 //*[contains(normalize-space(text()),"RowValueNameX")]

现在我需要得到这个文本

 RowValueX_ThatINeedToExtract

【问题讨论】:

    标签: xpath


    【解决方案1】:

    要选择包含要查找的文本的第一个divblock,可以使用:

    //div[contains(div, "RowValueNameX")]
    

    这给出了:

    <div class="row row-flex rowLine" style="padding: 2px 0;">
      <div class="col-1 vcenter">
        ::before
        <h4 style="white-space: nowrap;">
          RowValueNameX
        </h4>
      </div>
      <div class="col-2 vcenter text-right">
        ::before
        <h4>
          <b>RowValueX_ThatINeedToExtract</b>
        </h4>
      </div>
      ::after
    </div>
    

    您想要第二个div 孩子,所以完整的 XPath 如下所示:

    //div[contains(div, "RowValueNameX")]/div[2]
    

    这会导致:

    <div class="col-2 vcenter text-right">
      ::before
      <h4>
        <b>RowValueX_ThatINeedToExtract</b>
      </h4>
    </div>
    

    从这里应该很容易找到您真正想要的文本。

    【讨论】:

      猜你喜欢
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 2021-10-26
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多