gpt4 book ai didi

c# - 如何获取xpath中的最后一张表?

转载 作者:行者123 更新时间:2023-11-30 14:22:07 26 4
gpt4 key购买 nike

所以我有以下情况:

<table class="table-main detail-odds sortable">
..
</table>
<table class="table-main detail-odds sortable">
..
</table>

如您所见,我有两个具有相同类的表,我想获取最后一个表(我不能使用索引,因为表的编号在变化)。

目前我有这段代码:

HtmlNode oddsTable = doc.DocumentNode
.SelectNodes("//table[@class='table-main detail-odds sortable']");

不幸的是,我找不到任何.Last() 方法,也许可以直接使用xpath 而无需使用SelectNodes()

最佳答案

last() 仅当两个表都是同一父表的子表时,才会返回最后一个表。所以如果 HTML 真的看起来像

<table class="table-main detail-odds sortable">
..
</table>
<table class="table-main detail-odds sortable">
..
</table>

然后

//table[@class='table-main detail-odds sortable'][last()]

将获取所需的表...

万一

<div>
<table class="table-main detail-odds sortable">
..
</table>
</div>
<div>
<table class="table-main detail-odds sortable">
..
</table>
</div>

你可能需要

(//table[@class='table-main detail-odds sortable'])[count(//table[@class='table-main detail-odds sortable'])]

关于c# - 如何获取xpath中的最后一张表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51653717/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com