这是我的html文件
<div>
<div></div>
<div></div>
<!--Comment1-->
<div>1</div>
<div>2</div>
<div>3</div>
<!--Comment2-->
<div></div>
<div></div>
<div></div>
</div>
我想在评论 1 和评论 2 之间选择 div
有了这个 xPath = "/div/comment()"我可以选择 <!--Comment1--><!--Comment2-->
但是我要选择这个
<div>1</div>
<div>2</div>
<div>3</div>
最佳答案
//*[preceding-sibling::comment() and following-sibling::comment()]
或更严格的版本:
//*[preceding-sibling::comment()[. = 'Comment1']
and following-sibling::comment()[. = 'Comment2']]
关于python - 在lxml中的html注释之间选择div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8050929/