转: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类型