D
D
Dmitry Matveev2018-03-09 13:46:29
Python
Dmitry Matveev, 2018-03-09 13:46:29

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()

The first xpath outputs everything correctly. There are 4 divs in the html code.
Why then, when I search using xpath in a div, does xpath search again in the entire html code, and not relative to the given div? How to do it right?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question