Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select divs between html comments in lxml

This is my html file

<div>
   <div></div>
   <div></div>
   <!--Comment1-->
   <div>1</div>
   <div>2</div>
   <div>3</div>
   <!--Comment2-->
   <div></div>
   <div></div>
   <div></div>
</div>

and I want to select divs between Comment 1 and Comment 2

With this xPath = "/div/comment()" I can select <!--Comment1--><!--Comment2-->

But I want to select this

   <div>1</div>
   <div>2</div>
   <div>3</div>
like image 987
dani Avatar asked Jun 29 '26 06:06

dani


1 Answers

//*[preceding-sibling::comment() and following-sibling::comment()]

Or stricter version:

//*[preceding-sibling::comment()[. = 'Comment1'] 
    and following-sibling::comment()[. = 'Comment2']]
like image 146
Kirill Polishchuk Avatar answered Jun 30 '26 19:06

Kirill Polishchuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!