<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>
代码如下:
selector = etree.HTML(html)
content = selector.xpath('//div/table')[0]
print(content)
# <Element div at 0x1bce7463548>
# 即:如何将Element对象转成str类型