简体   繁体   English

如果H2包含某些文本,如何选择H3的内容

[英]How to select content of H3 if H2 contains certain text

I would like to scrape the content of the H3 element if the H2 just above it contains a certain text, in this case the H2 should contain "Course advanced Sydney". 如果H2元素正上方的H2包含特定文本,则我想抓取H3元素的内容,在这种情况下,H2应当包含“ Course advanced Sydney”。

But I can't get the Xpath to work. 但是我无法使Xpath正常工作。 Someone who can solve this issue? 谁能解决这个问题? Unfortunately I'm not very experienced with Xpath. 不幸的是,我对Xpath并不十分了解。 But I understand the basics. 但我了解基本知识。

I tried this with Xpath: 我用Xpath尝试了这个:

//div[@id='training']/article/[h2/text() = 'Course advanced Sydney']/h3

The code: 编码:

    <div id="training" class="overview">
        <article>
            <h2>Course beginner Amsterdam</h2>
            <h3>20 May 2019</h3>
        </article>
        <article>
            <h2>Course advanced Sydney</h2>
            <h3>27 May 2019</h3>
        </article>
        <article>
            <h2>Course beginner Sydney</h2>
            <h3>6 June 2019</h3>
        </article>
        <article>
            <h2>Course medior New York</h2>
            <h3>16 June 2019</h3>
        </article>
    </div>

I expect the output to be "27 May 2019", but the actual output is "#N/A" 我希望输出为“ 2019年5月27日”,但实际输出为“#N / A”

The h3 element is not a descendant of the h2 , it is its sibling. h3元素不是h2的后代,而是它的同级。 So use 所以用

//div[@id='training']/article/h2[contains(., 'Course advanced Sydney']/following-sibling::h3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM