我尝试使用以下代码从该问题末尾的输出中提取一些字段:
doc = LH.fromstring(html2)
tds = (td.text_content() for td in doc.xpath("//td[not(*)]"))
for a,b,c in zip(*[tds]*3):
print (a,b,c)
我期望的是只提取字段notificationNodeName,notificationNodeName,packageName,notificationEnabled
主要问题是因为我想将结果放入数据库。我需要,而不是收到:
实际代码返回:
('JDBCAdapter', 'JDBCAdapter', 'Package:Notif')
('Package', 'yes', 'Package_2:Notif')
('Package_2', 'yes')
我需要的:
('Package:Notif','Package', 'yes')
('Package_2:Notif','Package_2', 'yes')
我发现的一个不合理的解决方案是:
doc = LH.fromstring(html2)
tds = (td.text_content() for td in doc.xpath("//td"))
for td, val in zip(*[tds]*2):
if td == 'notificationNodeName':
notificationNodeName = val
elif td == 'packageName':
packageName = val
elif td == 'notificationEnabled':
notificationEnabled = val
print (notificationNodeName,packageName,notificationEnabled)
它有效,但不适合我,我相信这可能是一种更好的方法。
原始 HTML 输出:
<tbody><tr>
<td valign="top"><b>adapterTypeName</b></td>
<td>JDBCAdapter</td>
</tr>
<tr>
<td valign="top"><b>adapterTypeNameList</b></td>
<td>
<table>
<tbody><tr>
<td>JDBCAdapter</td>
</tr>
</tbody></table>
</td>
</tr>
<tr>
<td valign="top"><b>notificationDataList</b></td>
<td>
<table>
<tbody><tr>
<td><table bgcolor="#dddddd" border="1">
<tbody><tr>
<td valign="top"><b>notificationNodeName</b></td>
<td>package:Notif</td>
</tr>
<tr>
<td valign="top"><b>packageName</b></td>
<td>Package</td>
</tr>
<tr>
<td valign="top"><b>notificationEnabled</b></td>
<td>unsched</td>
</tr>
</tbody></table>
</td>
</tr>
<tr>
<td><table bgcolor="#dddddd" border="1">
<tbody><tr>
<td valign="top"><b>notificationNodeName</b></td>
<td>Package_2:notif</td>
</tr>
<tr>
<td valign="top"><b>packageName</b></td>
<td>package_2</td>
</tr>
<tr>
<td valign="top"><b>notificationEnabled</b></td>
<td>yes</td>
</tr>
and continues to more ... non relevant repetitive data.