如何编写从以下示例中选择表格的Xpath+可用的良好Xpath备忘单


How to write the Xpath for selecting a table from the following example + good Xpath cheatsheet available?

需要一些关于Xpath的说明。在HTML方面,我有以下内容。如何提取第一段后面的表格中的图像?

<p align="center">
    <img src="some_image.gif" alt="Some Title">
</p>
<table width="500" border="1" class="textstyle" align="center" cellpadding="0" cellspacing="0">
    <tr>
        <td colspan="4" align="center">
            <b>Label</b>
        </td>
    </tr>
    <tr>
        <td align="center">
            Text
        </td>
        <td align="center">
            Text
        </td>
        <td align="center">
            Text
        </td>
        <td align="center">
            Text
        </td>
    </tr>
</table>
<blockquote>
    <p class="textstyle">
        Text.
    </p>
</blockquote>

显然,我想看看如何为此编写xpath,但我想通过某种缩写列表/备忘单来进一步了解我的选项,了解xpath的选项。我正在考虑使用类似这样的东西:

//table[preceding-sibling::p[contains(align(), "center")

或者,也许我可以说一个以img为中心的p,但我只是不确定从哪里可以得到一个很好的全面列表,而不是一个解释所有血腥细节的实际文章。

使用

(//p[img])[1]/following-sibling::table[1]

这将选择table,它是文档中具有img子级的第一个p的第一个继table之后的同级。

我建议使用XPath Visualizer,这个工具已经帮助成千上万的人以有趣的方式学习XPath