简体   繁体   中英

Using XPath with lxml.html

I haven't used XPath much, so bear with me. I have an HTML file that contains two forms, each of which contains some input/select elements.

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 :

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

in your for loop.

e is a node attached to the whole document. When you perform XPath on it, you should stay in this context. When you use // , you are in the document context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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