2016-11-11 265 views
2
<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()') 

我什麼也沒有得到它。

回答

3

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

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

謝謝。它有我想要的數據,但它也包含另外兩個元素。請參閱上面編輯的代碼。 – vindex

+0

@vindex好的,請嘗試以下內容:'// th [包含(span,「EPS」)]/following-sibling :: td [1]/strong/text()',希望有效。 – alecxe