使用 XPath 获取样式值大于阈值的元素
Get elements with style value greater than threshold with XPath
所以,简而言之,给出以下 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
属性比给定的欢迎节点阈值,例如:/div[@style("left") < 300]
.
环顾四周似乎是不可能的,我设法找到的最接近的是 this,但是我想避免使用正则表达式来匹配数字数据,因为阈值可能会有所不同。
我正在尝试通过 Python(lxml
模块)提取此信息。基本上我有一个 pdf,左右两列,我想将页面分成 2 个(单独获取左侧的所有内容,单独获取右侧的所有内容)。
试试这个:
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]
这将选择第一个 <div>
。
所以,简而言之,给出以下 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
属性比给定的欢迎节点阈值,例如:/div[@style("left") < 300]
.
环顾四周似乎是不可能的,我设法找到的最接近的是 this,但是我想避免使用正则表达式来匹配数字数据,因为阈值可能会有所不同。
我正在尝试通过 Python(lxml
模块)提取此信息。基本上我有一个 pdf,左右两列,我想将页面分成 2 个(单独获取左侧的所有内容,单独获取右侧的所有内容)。
试试这个:
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]
这将选择第一个 <div>
。