gpt4 book ai didi

python - 创建 xpath 以便能够有条件地定位元素时遇到问题

转载 作者:行者123 更新时间:2023-12-01 01:21:32 26 4
gpt4 key购买 nike

我一直在尝试创建一个 xpath 来定位前三个 Yes p内元素直到文本 Demarcation h1内元素。我在下面的脚本中使用的现有脚本可找到 p 中的所有文本。元素。但是,我找不到任何继续前进的想法。只需将我已经创建的占位符视为占位符即可。

如何创建一个 xapth 以便能够找到前三个 Yes p内元素而不是其他?

到目前为止我的尝试:

from lxml.html import fromstring

htmldoc="""
<li>
<a>Nope</a>
<a>Nope</a>
<p>Yes</p>
<p>Yes</p>
<p>Yes</p>
<h1>Demarcation</h1>
<p>No</p>
<p>No</p>
<h1>Not this</h2>
<p>No</p>
<p>Not this</p>
</li>
"""
root = fromstring(htmldoc)
for item in root.xpath("//li/p"):
print(item.text)

最佳答案

尝试在下面选择标题“Demarcation”前面的同级段落

//li/p[following-sibling::h1[.="Demarcation"]]

关于python - 创建 xpath 以便能够有条件地定位元素时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53793737/

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