Lxml html xpath 上下文

发布于 2024-08-11 00:09:31 字数 352 浏览 4 评论 0原文

我正在使用 lxml 解析 HTML 文件,我想知道如何设置 xpath 搜索的 context 。我的意思是,我有一个节点元素,并且希望仅在该节点内部进行 xpath 搜索,就好像它是根节点一样。例如,我有一个表单节点,xpath 搜索 //input 仅返回给定表单的输入,而不是页面上所有表单的所有输入。我该怎么做?我在这里找到了一些xpath context文档,但这似乎不是我想要的。

I'm using lxml to parse a HTML file and I'd like to know how I can set the context of xpath search. What I mean is that I have a node element and want to make xpath search only inside this node as if it was the root one. For example, I have a form node and xpath search //input returns only inputs of the given form as opposed to all inputs of all forms on the page. How can I do this? I've found some xpath context docs here, but it doesn't seem to be quite what I want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若沐 2024-08-18 00:09:31

XPath 表达式 //input 将匹配文档中任何位置的所有输入元素,而 .//input 将匹配当前上下文中的所有输入元素。

也许如果您改进场景描述,我们可以为您提供进一步帮助。

XPath expression //input will match all input elements, anywhere in your document, while .//input will match all inside current context.

Maybe if you improve your scenario description we can help you further.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文