XPath提取属性中的值

时间:2018-11-07 20:28:21

标签: xpath attributes extract

到目前为止,这是我的HTML代码:

<tr valign="top">
    <td nowrap="x">Citation(s)</td>
    <td>
        <span class="pubmed_id" id="26472973">
            <a href="/pubmed/26472973">26472973</a>
        </span>
    </td>
</tr>

我想提取数字26472973,该数字对于数据库中的每个条目都会改变。

2 个答案:

答案 0 :(得分:1)

不清楚是要从属性@id还是从后面的a元素中获取值。
因此,对于属性值,请尝试以下XPath:

//tr[@valign='top']/td/span[@class='pubmed_id']/@id

或者,对于元素的a值,请使用以下XPath:

//tr[@valign='top']/td/span[@class='pubmed_id']/a/text()

在两种情况下,结果均为26472973

答案 1 :(得分:0)

如果您只想要“引用”,请尝试以下操作:

//tr/td[text()='Citation(s)']/following-sibling::td/span/@id
相关问题