【问题标题】:Get elements with style value greater than threshold with XPath使用 XPath 获取样式值大于阈值的元素
【发布时间】:2020-03-25 18:40:15
【问题描述】:

所以,简而言之,给定以下 html(额外的星号是我自己添加的):

<div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; **left:66px;** top:1892px; width:91px; height:10px;">
    <span style="font-family: Times-Roman; font-size:10px">FOO  
    <br>
    </span>
</div>
<div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; **left:514px;** top:1892px; width:20px; height:10px;">
    <span style="font-family: Times-Roman; font-size:10px">BAR
    <br>
    </span>
</div>

我想利用 X-Path 来获取所有具有left 属性小于阈值的节点,并获取所有具有left 属性greeter 的节点小于给定阈值,例如这样:/div[@style("left") &lt; 300].

环顾四周似乎是不可能的,我设法找到的最接近的是this,但是我想避免使用正则表达式来匹配数字数据,因为阈值可能会有所不同。

我正在尝试通过 Python(lxml 模块)提取此信息。基本上我有一个左右两列的pdf,我想将页面分成2个(自己获取左侧的所有内容,以及右侧的所有内容)。

【问题讨论】:

  • 环顾四周似乎是不可能的我很困惑,那么你的问题是什么?

标签: python python-3.x pdf lxml


【解决方案1】:

试试这个:

import lxml.html
foo = """
<div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:66px; top:1892px; width:91px; height:10px;">
    <span style="font-family: Times-Roman; font-size:10px">FOO  
    <br>
    </span>
</div>
<div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:514px; top:1892px; width:20px; height:10px;">
    <span style="font-family: Times-Roman; font-size:10px">BAR
    <br>
    </span>
</div> """

doc = lxml.html.fromstring(foo)
doc.xpath("//div[number(substring-before(substring-after(@style, 'left:'),'px;')) < 300]")[0]

这会选择第一个&lt;div&gt;

【讨论】:

    猜你喜欢
    • 2016-10-24
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 2022-12-18
    • 1970-01-01
    相关资源
    最近更新 更多