xpath与lxml for Python获取数据
问题描述:
<th><span class="sic_edu_series_popup {keyword : 'EPS_STOCK'}">EPS</span>
(SGD) <sup class="sic_legend">a
, j
</sup></th>
<td><strong>1.89766</strong></td>
<th><span class="sic_edu_series_popup {keyword : 'TRAILING_EPS_STOCK'}">Trailing EPS</span>
(SGD) <sup class="sic_legend">e</sup></th>
<td><strong>1.87198</strong></td>
<th><span class="sic_edu_series_popup {keyword : 'NAV_STOCK'}">NAV</span>
(SGD) <sup class="sic_legend">b</sup></th>
<td><strong>18.5449</strong></td>
</tr>
我正在尝试为'尾随EPS'提取数据以获取数据'1.87198'。有很多这种格式的数据与不同的名称,如EPS,ROE等xpath与lxml for Python获取数据
tree.xpath('//th[contains(normalize-space(span), "EPS")]/sup[@class = "sic_legend"]/td/text()')
我什么也没有得到它。
答
td
元素不是sup
元素的子元素。使用的事实,th
和td
是兄弟姐妹:
//th[contains(span, "EPS")]/following-sibling::td/strong/text()
谢谢。它有我想要的数据,但它也包含另外两个元素。请参阅上面编辑的代码。 – vindex
@vindex好的,请尝试以下内容:'// th [包含(span,“EPS”)]/following-sibling :: td [1]/strong/text()',希望有效。 – alecxe