2

这个问题可能有点具体,但我正在编写的测试程序使用 XPath 在 HTML 中查找我需要的数据。这段 HTML(在此处找到)是我试图解析的内容。

<table border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td class="textSm" align="right">1.&nbsp;</td> <!-- Location of the number is here -->
        <td align="left" nowrap>
            <a href="/stats/individual_stats_player.jsp?c_id=sf&playerID=467055" class="textSm">P Sandoval</a> <!-- Player location is here of the number is here -->
        </td>
    </tr>
</table>

我的目标是通过使用与他对应的数字来找到他的名字。这需要我通过“”中包含的特定文本找到一个节点,td class="textSm" align="right">1. </td>然后找到该节点的兄弟“”,然后找到该兄弟<td align="left" nowrap>的孩子“ <a href="/stats/individual_stats_player.jsp?c_id=sf&playerID=467055" class="textSm">P Sandoval</a>”以获得所需的结果。我想知道我可以使用什么样的查询来找到这个。很感谢任何形式的帮助。

4

2 回答 2

1

使用

table/tr/td[starts-with(., '1.')]/following-sibling::td/a

这假定上面的 XPath 表达式所针对的上下文(当前节点)是table.

于 2010-07-01T15:42:21.977 回答
0
//tr[td = "1"]/td[2]/a

对于所有 TD 等于 '1' 的 TR,从第二个子 TD 中给出 A 元素。

于 2010-07-01T06:31:18.857 回答