如何找到介于两个类型B的XPath之间的类型A的XPath的数量?

2024-10-02 00:40:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个在线曲棍球比赛摘要中获取关于比赛期间所评估的处罚的信息

每种惩罚都有以下特点:

[…]/div/div[x]/h4

其中x是惩罚的次数

在这两者之间,有惩罚发生期间的标签,其中包含xpath:

[…]/div/h3[y]

其中y是周期的编号

问题是,新周期的开始不会重置惩罚数,总结如下:

First period ([…]/div/h3[1])  
Penalty1 ([…]/div/div[1]/h4)  
Penalty2 ([…]/div/div[2]/h4)  
Second period ([…]/div/h3[2])  
Penalty3 ([…]/div/div[3]/h4)  
Penalty4 ([…]/div/div[4]/h4)  
Penalty5 ([…]/div/div[5]/h4)  
Third period ([…]/div/h3[3])  
Penalty6 ([…]/div/div[6]/h4)  

显然,不同的比赛有不同的罚球数

有没有办法让我知道在一场比赛的每一个阶段中,有多少个点球被判罚?
在上面的例子中,我想知道第一节有2次罚球,第二节有3次,第三节有1次

理论上我可以使用CSS选择器,但我不确定如何使用它们: 每一个惩罚都有一个选择符

div class=“boxscore-event-ctn"

每个周期都有CSS选择器

h3 class=“margT40"

但是,这些相同的CSS选择器也用于HTML代码中的其他元素

谢谢

编辑:下面是一个示例游戏的HTML相关部分。 这场比赛第一节没有点球,第二节有3个点球,第三节有2个点球:

      <div id="tabcontent102">
        <div class="pad40" style="background-image:none;">
          <h2>Penalties summary</h2>
          <h3 class="margT40">First Period</h3><span>No penalty</span>  
          <h3 class="margT40">Second period</h3>
          <div class="boxscore-event-ctn"><img style="width:40px;height:40px;float:left;"src="https://cdn.wssmedias.com/websimhockey/logos/LHSVL/Toronto.jpg">. 
            <h4>15:26 T. BERTUZZI (Rudesse)</h4>
            <div class="flowfix"></div>
          </div>
          <div class="boxscore-event-ctn"><img style="width:40px;height:40px;float:left;"src="https://cdn.wssmedias.com/websimhockey/logos/LHSVL/Toronto.jpg">. 
            <h4>09:40 M. CARLE (Avoir retenu)</h4>
            <div class="flowfix"></div>
          </div>
          <div class="boxscore-event-ctn"><img style="width:40px;height:40px;float:left;"src="https://cdn.wssmedias.com/websimhockey/logos/LHSVL/SanDiego.jpg">. 
            <h4>05:58 J. JOHNSON (Faire trébucher)</h4>
            <div class="flowfix"></div>
          </div>
          <h3 class="margT40">Third period</h3>
          <div class="boxscore-event-ctn"><img style="width:40px;height:40px;float:left;"src="https://cdn.wssmedias.com/websimhockey/logos/LHSVL/SanDiego.jpg"> 
            <h4>13:33 J. BOLL (Trop de joueurs sur la glace - banc)</h4>
            <div class="flowfix"></div>
          </div>
          <div class="boxscore-event-ctn"><img style="width:40px;height:40px;float:left;"src="https://cdn.wssmedias.com/websimhockey/logos/LHSVL/SanDiego.jpg">  
            <h4>06:36 B. BURNS (Avoir accroché)</h4>
            <div class="flowfix"></div>
          </div>
          <h3 class="margT40">Overtime</h3><span>No penalty</span></div>
      </div>

Tags: diveventimgstylefloatwidthlefth4
1条回答
网友
1楼 · 发布于 2024-10-02 00:40:52

这个答案显示了如何使用XPath在两个已知元素之间获取信息。
请注意,XPath 1.0由许多语言实现,因此显示的结果基于XPath表达式,而不是语言或实用程序(在本例中为xmllint

获取第二节课的罚分计数

XPath

全部在一行中
count(//div[@id="tabcontent102"]//div[@class="boxscore-event-ctn" and ./preceding-sibling::h3[.="Second period"] and not(./preceding-sibling::h3[.="Third period"])]/h4)

xmllint测试

xmllint  html  xpath 'count(//div[@id="tabcontent102"]//div[@class="boxscore-event-ctn" and ./preceding-sibling::h3[.="Second period"] and not(./preceding-sibling::h3[.="Third period"])]/h4)' test.html ; echo

结果:

3

要获得第三个句点,请更改谓词中的测试值
count(//div[@id="tabcontent102"]//div[@class="boxscore-event-ctn" and ./preceding-sibling::h3[.="Third period"] and not(./preceding-sibling::h3[.="Overtime"])]/h4)

使用元素位置而不是值

Xpath

//div[@id="tabcontent102"]//div[@class="boxscore-event-ctn" and ./preceding-sibling::h3[3] and not(./preceding-sibling::h3[4])]/h4/text()

在命令行上获取h4文本值

(echo 'cat //div[@id="tabcontent102"]//div[@class="boxscore-event-ctn" and ./preceding-sibling::h3[3] and not(./preceding-sibling::h3[4])]/h4/text()') | xmllint  html  shell test.html 

结果:

/ > cat //div[@id="tabcontent102"]//div[@class="boxscore-event-ctn" and ./preceding-sibling::h3[3] and not(./preceding-sibling::h3[4])]/h4/text()
    -
13:33 J. BOLL (Trop de joueurs sur la glace - banc)
    -
06:36 B. BURNS (Avoir accroch&Atilde;&copy;)

得到最后一个元素的计数

Xpath

count(//div[@id="tabcontent102"]//h3[.="Overtime"]/following-sibling::div[@class="boxscore-event-ctn"])

使用h3位置
count(//div[@id="tabcontent102"]//h3[4]/following-sibling::div[@class="boxscore-event-ctn"])

相关问题 更多 >

    热门问题