1

我尝试使用以下代码从该问题末尾的输出中提取一些字段:

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.
4

2 回答 2

2

我建议使用优秀的 lxml 和它的 cssselect 功能,基本上大多数 HTML 解析。

然后,您可以选择您感兴趣的每个字段:

from lxml import html
root = html.parse(open('your/file.html')).getroot()

sibling_content = lambda x: [b.getparent().getnext().text_content() for b in
                             root.cssselect("td b:contains('{0}')".format(x))]

fields = ['notificationNodeName', 'packageName', 'notificationEnabled']

for item in zip(*[sibling_content(field) for field in fields]):
    print item
于 2012-08-23T16:23:14.503 回答
1

我还推荐 lxml - 它是使用 Python 解析 XML 或 HTML 的事实标准。

作为 David 方法的替代方案,这里有一个使用 xpaths 的解决方案:

from lxml import html
from lxml import etree


html_file = open('test.html', 'r')
root = html.parse(html_file).getroot()

# Strip those annoying <b> tags for easier xpaths
etree.strip_tags(root,'b')

data_list = root.xpath("//td[text()='notificationDataList']/following-sibling::*")[0]

node_names = data_list.xpath("//td[text()='notificationNodeName']/following-sibling::*/text()")
package_names = data_list.xpath("//td[text()='packageName']/following-sibling::*/text()")
enableds = data_list.xpath("//td[text()='notificationEnabled']/following-sibling::*/text()")

print zip(node_names, package_names, enableds)

输出:

[('package:Notif', 'Package', 'unsched'),
 ('Package_2:notif', 'package_2', 'yes')]
于 2012-08-23T16:42:10.573 回答