简体   繁体   English

将XPath与lxml.html一起使用

[英]Using XPath with lxml.html

I haven't used XPath much, so bear with me. 我没有太多使用XPath,所以请多多包涵。 I have an HTML file that contains two forms, each of which contains some input/select elements. 我有一个HTML文件,其中包含两种形式,每种形式都包含一些输入/选择元素。

In [146]: len(doc.xpath('//input | //select'))
Out[146]: 14

In [147]: len(doc.xpath('//form'))
Out[147]: 2

Is there a way to loop through forms and find respective input / select elements? 有没有一种方法可以遍历表格并找到相应的输入/选择元素? At the moment it returns all the elements twice. 此刻,它两次返回所有元素。

In [149]: for e in doc.xpath('//form'):
     ...:     print len(e.xpath('//input | //select'))
     ...:     
14
14

I dont know XPath integration in python, but I think you can try : 我不知道python中的XPath集成,但是我认为您可以尝试:

e.xpath('.//input | .//select')

in your for loop. 在您的for循环中。

e is a node attached to the whole document. e是附加到整个文档的节点。 When you perform XPath on it, you should stay in this context. 在其上执行XPath时,应保持在此上下文中。 When you use // , you are in the document context. 使用// ,您位于文档上下文中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM