如何获取父元素然后获取下一个元素

时间:2017-10-04 02:42:06

标签: html xpath scrapy

我想获取指定元素的父级(带有类的span是mw-headline),然后获取该父级的第一个下一个元素。

<h2>
    <span class="mw-headline" id="Botany">Botany
    </span>
    <span class="mw-editsection">
        <span class="mw-editsection-bracket">
        </span><a href="/w/index.php?title=Apple_(disambiguation)&amp;action=edit&amp;section=1&amp;editintro=Template:Disambig_editintro" title="Edit section: Botany">edit</a><span class="mw-editsection-bracket">

    </span>
</span>
</h2>
<ul>
    <li><i><a href="/wiki/Malus" title="Malus">Malus</a></i>, the genus of all apples and crabapples</li>
    <li><a href="/wiki/Cashew_apple" class="mw-redirect" title="Cashew apple">Cashew apple</a>, the fruit that grows with the cashew nut</li>
    <li><a href="/wiki/Custard_apple" title="Custard apple">Custard apple</a>, several fruits</li>
    <li><a href="/wiki/Love_apple" title="Love apple">Love apple</a>:
        <ul>
            <li><a href="/wiki/Tomato" title="Tomato">Tomato</a></li>
            <li><i><a href="/wiki/Syzygium_samarangense" title="Syzygium samarangense">Syzygium samarangense</a></i>, a plant species in the Myrtaceae family</li>
        </ul>
    </li>
    <li><a href="/wiki/Mammee_apple_(disambiguation)" class="mw-redirect" title="Mammee apple (disambiguation)">Mammee apple (disambiguation)</a></li>
    <li><a href="/wiki/May_apple" class="mw-redirect" title="May apple">May apple</a> (<i>Podophyllum peltatum</i>)</li>
    <li><a href="/wiki/Oak_apple" title="Oak apple">Oak apple</a>, a type of gall that grows on oak trees</li>
    <li><a href="/wiki/Rose_apple_(disambiguation)" class="mw-redirect" title="Rose apple (disambiguation)">Rose apple (disambiguation)</a>, several fruits</li>
    <li><a href="/wiki/Thorn_apple_(disambiguation)" class="mw-redirect mw-disambig" title="Thorn apple (disambiguation)">Thorn apple (disambiguation)</a>:
        <ul>
            <li><i><a href="/wiki/Crataegus" title="Crataegus">Crataegus</a></i> species</li>
            <li><i><a href="/wiki/Datura" title="Datura">Datura</a></i> species</li>
        </ul>
    </li>
    <li><a href="/wiki/Wax_apple" class="mw-redirect" title="Wax apple">Wax apple</a> (<i>Syzygium samarangense</i>)</li>
    <li><a href="/wiki/Hedge_apple" class="mw-redirect" title="Hedge apple">Hedge apple</a> (<i>Maclura pomifera</i>)</li>
</ul>

我希望在h2标签指定span后面的第一个ul,类是mv-headline。

从xpath开始,我的解决方案非常简单:

$x('//span[@class="mw-headline"]/following::ul[1]')

但我不知道在我的情况下如何选择get with parent(在xpath中)和下一个元素(在xpath中的:: node)。 请通过CSS选择器给我一个解决方案。

谢谢&amp;最诚挚的问候,

Phuong Hoang

1 个答案:

答案 0 :(得分:-1)

你在XPath中犯了一个错误,xpath应该是:

$x('//h2[//span[@class="mw-headline"]]/following::ul[1]')