python - 在lxml中的html注释之间选择div

标签 python html xml parsing xpath

这是我的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/

相关文章:

html - Div 在屏幕尺寸变化时移动

html - 将 CSS 菜单移到屏幕右侧?

python - 如果满足条件则替换行中的单元格

python - 构建 python 包

python - Django/Python - 检查用户是否上传文件 - 如果没有,显示图像

xml - 在 Xml 变量中存储查询结果时如何保留 CDATA 标记?

java - 文档 - 如何通过名称获取标签的值?

python - 在 Django 模型中的一个变量中存储多个信息

javascript - 不需要的 MobileSafari 'Open this page in "App Store”?对话

XPath 值的 XML 架构类型定义?