gpt4 book ai didi

Python + XPath : Is it possible to select the next element after the one I actually want?

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

假设我有这样的东西:

<span class="filesize">File<a href="http://example.com/image.jpg" 
target="_blank">image.jpg</a>-(1.61 MB, 1000x1542, <span title="what the file is actually
called.jpg">what the file is actually called.jpg</span>)</span><br><a href="http://example.com
/image.jpg" target="_blank">

我想从中提取的是 http://example.com/image.jpgwhat the file is actually called.jpg 。常数项是<span class="filesize">File我可以使用 xpath("span[text()='File']") 找到它但这只能让我访问 span 。有没有办法做类似 result += 1 的事情然后转到链接,然后访问 span后面加上文件名?

最佳答案

您可以使用following-siblingpreceding-sibling xpath“轴”来进行您需要的导航。您可以获取拘留here .

编辑:

这是一个仅使用 xpath 即可获得您想要的结果的示例。然而,它可能对你不起作用,具体取决于周围的 XML 是什么样的:(我还必须完成一些标签才能成为“真正的”XML。你可以通过将你的 XML解析器进入 HTML 模式...)

import lxml.etree

xml = lxml.etree.XML("""<something><span class="filesize">File<a href="http://example.com/image.jpg" target="_blank">image.jpg</a>-(1.61 MB, 1000x1542, <span title="what the file is actually called.jpg">what the file is actually called.jpg</span>)</span><br/><a href="http://example.com/image.jpg" target="_blank"></a></something>""",)

print xml.xpath("a[preceding-sibling::span/text()='File']/@href")

关于Python + XPath : Is it possible to select the next element after the one I actually want?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7466013/

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