LXML HTML XPath上下文 [英] Lxml html xpath context

查看:81
本文介绍了LXML HTML XPath上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using lxml to parse a HTML file and I'd like to know how can I set the context of xpath search. What I mean I 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 return only inputs of the given form as opposed to all inputs of all forms on the page. How can I do that? I've found some xpath context docs here, but it doesn't seems to be quite what I want.

推荐答案

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.

这篇关于LXML HTML XPath上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆