如何使用xpath在html代码中查找和选择表

如何使用xpath在html代码中查找和选择表,xpath,python-2.7,lxml,Xpath,Python 2.7,Lxml,我是python和xpath新手, 我有如下html代码: <a name="hello"></a> <h3>hello</h3> <table /> <a name="impact"></a> <h3>Impact</h3> <table cellspacing="0" cellpadding="0" border="0" class="wrapper-table"><

我是python和xpath新手, 我有如下html代码:

<a name="hello"></a>
<h3>hello</h3>
<table />

<a name="impact"></a>
<h3>Impact</h3>
<table cellspacing="0" cellpadding="0" border="0" class="wrapper-table"><tr> <td><p>An     unauthenticated attacker using a specifically crafted payload may be able to trick the Ruby on Rails backend into executing arbitrary code.</p></td></tr></table>

你好
影响
使用特制负载的未经验证的攻击者可能会欺骗Ruby on Rails后端执行任意代码

我想保存整个表及其所有标记和文本,然后。。。一串。 我想要冲击头后面的表标记

使用

tables = root.xpath('.//table[preceding-sibling::h3[text()="Impact"]]')

完全解
:谢谢您的回复,但我仍然不知道如何以字符串形式保存表@NiloofarKhalilian,
lxml.html.tostring(root)
将为您提供html字符串。我需要此字符串str=未经验证的攻击者使用特制的负载可能会诱使Ruby on Rails后端执行任意代码。

我如何获得此结果?@NiloofarKhalilian,您阅读了我的评论吗?:(是的,我读过它,但我对python非常陌生。这是我的代码:root=tree.getroot()tables=root.xpath('.//h3[text()=“Impact”]/following sibling::table')str=etree.tostring(tables)print str,但我得到以下错误:无法序列化类型'list'
tables = root.xpath('.//h3[text()="Impact"]/following-sibling::table')
tables = root.cssselect('h3:contains(Impact) ~ table')
root = tree.getroot()
tables = root.xpath('.//h3[text()="Impact"]/following-sibling::table')
for table in tables:
    print str