Lxml html xpath 上下文
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.