轉:https://segmentfault.com/q/1010000012110138/a-1020000012113020
<div>
<table>
<tr>
<td class="td class">Row value 1</td>
<td class="td class">Row value 2</td>
</tr>
<tr>
<td class="td class">Row value 3</td>
<td class="second td class">Row value 4</td>
</tr>
<tr>
<td class="third td class">Row value 1</td>
<td class="td class">Row value 1</td>
</tr>
</table>
</div>
如何把table標簽提取出來,結果如下:
<table>
<tr>
<td class="td class">Row value 1</td>
<td class="td class">Row value 2</td>
</tr>
<tr>
<td class="td class">Row value 3</td>
<td class="second td class">Row value 4</td>
</tr>
<tr>
<td class="third td class">Row value 1</td>
<td class="td class">Row value 1</td>
</tr>
</table>
代碼如下:
tree = etree.HTML(html) content = tree.xpath('//div/table')[0] print(content)
# <Element div at 0x1bce7463548> # 即:如何將Element對象轉成str類型
