基于2个条件而不是在同一节点级别上选择xpath中的元素

问题描述 投票:0回答:1

更新:

我不知道使用以下结构过滤两个条件的语法。

我需要在名为

<a class="_1ufH4" href="ELEMENT of INTEREST">
 的节点之一中选择一个特定的 
<div class="_1rOLI _My0B">

仅当满足以下两个条件时:

  1. <h2 class="_2MeiE">PARIS</h2>

  2. <div class="_16U2O typography-h220">20:30</div>

<root>

    <div class="_13nA5">
    <section role="none" class="_29N96">
        <div class="SppyD">
            <div class="_2Bl6B">
                <img src="https://example" alt="" width="20" height="20">
                </div>

                <h2 class="_2MeiE">LONDON</h2>
            </div>

            <div class="_26Fte">


                <div class="_1rOLI _My0B">
                    <div class="_2VB9y">
                        <div class="_18IfB">
                            <div class="_3u6AO">
                                <a class="_1ufH4" href="ELEMENT of INTEREST">
                                    <div class="_2AdVd">
                                        <div class="Xi8qr">
                                            <div class="_16U2O typography-h220">14:30</div>
                                        </div>
                                    </div>
                                </a>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="_1rOLI _My0B">
                    <div class="_2VB9y">
                        <div class="_18IfB">
                            <div class="_3u6AO">
                                <a class="_1ufH4" href="ELEMENT of INTEREST">
                                    <div class="_2AdVd">
                                        <div class="Xi8qr">
                                            <div class="_16U2O typography-h220">15:30 2</div>
                                        </div>
                                    </div>
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
      

              </div>
            </div>
        </section>
    </div>




    <div class="_13nA5">
    <section role="none" class="_29N96">
        <div class="SppyD">
            <div class="_2Bl6B">
                <img src="https://example" alt="" width="20" height="20">
                </div>

                <h2 class="_2MeiE">PARIS</h2>
            </div>

            <div class="_26Fte">


                <div class="_1rOLI _My0B">
                    <div class="_2VB9y">
                        <div class="_18IfB">
                            <div class="_3u6AO">
                                <a class="_1ufH4" href="ELEMENT of INTEREST">
                                    <div class="_2AdVd">
                                        <div class="Xi8qr">
                                            <div class="_16U2O typography-h220">20:30</div>
                                        </div>
                                    </div>
                                </a>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="_1rOLI _My0B">
                    <div class="_2VB9y">
                        <div class="_18IfB">
                            <div class="_3u6AO">
                                <a class="_1ufH4" href="ELEMENT of INTEREST">
                                    <div class="_2AdVd">
                                        <div class="Xi8qr">
                                            <div class="_16U2O typography-h220">16:30</div>
                                        </div>
                                    </div>
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
      

              </div>
            </div>
        </section>
    </div>

<root/>

我遇到过“前面的兄弟姐妹”,但我不知道如何根据我给你的两个条件的位置来选择感兴趣的元素。

python xml web-scraping xpath
1个回答
0
投票

如果我理解正确的话

//section[contains(.,"PARIS")]//a[@class="_1ufH4" and contains (.//div,"20:30")]

您可以进一步自定义第一个 contains 参数以获得更多独特性

© www.soinside.com 2019 - 2024. All rights reserved.