2016-12-06 48 views
0

以前正在使用python的xpath,它對於從網頁提取數據是強大的。現在我需要爲同一個網頁使用YQL,但它不夠健壯。YQL xpath不夠健壯

我想是 1尾(AUD) 2.關閉 3.關閉(%) 4.累積量 從https://www.shareinvestor.com/fundamental/factsheet.html?counter=TPM.AX 我在Python使用XPath情況如下:

xpath('//td[contains(., "Last")]/strong/text()') 
xpath('//td[contains(., "Change")]/strong/text()')[0] 
xpath('//td[contains(., "Change (%)")]/strong/text()') 
xpath('//td[contains(., "Cumulative Volume")]/following-sibling::td[1]/text()') 
在HTML的

部分是這裏

<tr> 
       <td rowspan="2" class="sic_lastdone">Last (AUD): <strong>6.750</strong></td> 
       <td class="sic_change">Change: <strong>-0.080</strong></td> 
       <td>High: <strong>6.920</strong></td> 
       <td rowspan="2" class="sic_remarks"> 
        Remarks: <strong>-</strong> 
       </td> 
       </tr> 
       <tr> 
       <td class="sic_change">Change (%): <strong>-1.17</strong></td> 
       <td>Low: <strong>6.700</strong></td> 
       </tr> 
       <tr> 

<tr> 
       <td>Cumulative Volume (share)</td> 
       <td class='sic_volume'>3,100,209</td> 
       <td>Cumulative Value</td> 
       <td class='sic_value'></td> 
       </tr> 

但是,當我想在YQL申請,也沒有工作。它只與

select * from html where 
url="https://www.shareinvestor.com/fundamental/factsheet.html?counter=TPM.AX" 
and xpath="//td/strong" 

它會得到大量的數據。我想要一個特定的數據,並需要健壯,以便網頁的更改,我的查詢仍然工作。如何獲得強大的YQL xpath?

回答

0

您應該避免根據可見文本構建xpath。

我總是根據標籤屬性來構建xpath,因爲它們通常不會更改。這使得xpath結果是唯一的,並且不受HTML中可見文本更改的影響。

例如, 「上次(AUD):」 值的XPath: //td[@class="sic_lastdone"]/strong/text()

+0

只需找出相同的XPath答案像你。謝謝。屬性標籤很少更改? – vindex

+0

是的,它們基本上就像ID一樣。文本可能會更改(多語言支持站點),但屬性很少更改,因爲它們不可見 – Derorrist