XPath for next / sibling / follow HTML元素?

时间:2015-05-04 23:19:39

标签: html xml r xpath web-scraping

在以下HTML代码中,我有两个嵌套的<href>链接:

<a href="/cgi-bin/WebOb/mamool/8.2">
<img width="12" border="0" align="ABSMDIDDLE" height="7" src="/WebOb/mamool/Frameworks/fig.gif">
Click me for more info
</a>
<table border="1" size="2" font="">
<tbody>
<tr>
<td>
<font size="2">
<a name="179"></a>
    <a href="/cgi-bin/WebOb/mamool/8.2.44">
    <img width="12" border="0" align="ABSMDIDDLE" height="7" src="/WebOb/mamool/Frameworks/myfig.gif">
</a>
</td>
</tr>
<tr bgcolor="#d6e2ff">
<td>
</tr>
</tbody>
</table>

我可以轻松找到第一个<href>链接的XPath:

//a[contains(text(), 'Click me for more info')]

现在我想知道如何在不搜索的情况下找到下一个<href>,只需说出像.next()这样的内容?

1 个答案:

答案 0 :(得分:2)

可以使用following-sibling轴选择下一个兄弟元素:

//a[contains(text(), 'Click me for more info')]/following-sibling::*[1]

会选择示例中的table元素。

如果要选择文档中的下一个a元素,请使用following轴:

//a[contains(text(), 'Click me for more info')]/following::a[1]
相关问题