Answer the question
In order to leave comments, you need to log in
How does xpath work in lxml?
Hello, there was a misunderstanding how xpath works in lxml.
import lxml.html as parser
text = '''
<div id="main">
<div>
<a href="#">1</a>
</div>
<div>
<a href="#">2</a>
</div>
<div>
<a href="#">3</a>
</div>
<div>
<a href="#">4</a>
</div>
</div>
'''
def get_info():
html = parser.document_fromstring(text)
divs = html.xpath('//div[@id="main"]/div')
print(len(divs))#4
div = divs[0].xpath('///a')
print(len(div))#4
if __name__ == '__main__':
get_info()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question