2
這個問題聽起來很容易,但我在解決它時遇到了困難。我有這樣一個如下表:在Python中刮掉HTML表lxml
<table><tbody>
<tr>
<td>2003</td>
<td><span class="positive">1.19</span> </td>
<td><span class="negative">-0.48</span> </td>
</tr>
我的代碼如下:
from lxml import etree
for elem in tree.xpath('//*[@id="printcontent"]/div[8]/div/table/tbody/tr'):
for c in elem.xpath("//td"):
if(c.getchildren()): # for the <span> thing
text = c.xpath("//span/text()")
else:
text = c.text
但我無法遍歷「TD」的元素。我一直在嘗試這一整天但無濟於事!我想得到2003. 1.19和-0.48。
請幫忙!
bravo!是的,我犯了這個XML - HTML錯誤 – user3001408 2014-12-06 13:41:35