用于Python的lxml的xpath来获取数据

时间:2016-11-11 14:34:09

标签: python xpath lxml

<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>

我正在尝试提取&#39;追踪EPS&#39;获取数据&#39; 1.87198&#39;。这种格式有许多不同名称的数据,如EPS,ROE等

tree.xpath('//th[contains(normalize-space(span), "EPS")]/sup[@class = "sic_legend"]/td/text()')

我什么都没得到。

1 个答案:

答案 0 :(得分:3)

td元素不是sup元素的子元素。使用thtd 兄弟姐妹的事实:

//th[contains(span, "EPS")]/following-sibling::td/strong/text()