从表中提取行

时间:2015-11-30 16:22:13

标签: python xpath lxml

我正在尝试使用下表中的相应单元格提取行:

<table border="0" cellspacing="1" cellpading="3" width="100%">
<tr bgcolor="#505050">
    <td><b></b></td>
    <td colspan="2" align="center" class="white"><b>Last Day</b></td>
    <td colspan="2" align="center" class="white"><b>Last Week</b></td>
</tr>
<tr bgcolor="#505050">
    <td class="white"><b>Race</b></td>
    <td align="center" class="white"><b>Killed Players</b></td>
    <td align="center" class="white"><b>Killed by Players</b></td>
    <td align="center" class="white"><b>Killed Players</b></td>
    <td align="center" class="white"><b>Killed by Players</b></td>
</tr>
<tr bgcolor="#F1E0C6">
    <td>A</td>
    <td align="right">0</td>
    <td align="right">3</td>
    <td align="right">0</td>
    <td align="right">13</td>
</tr>
<tr bgcolor="#D4C0A1">
    <td>B</td>
    <td align="right">0</td>
    <td align="right">0</td>
    <td align="right">0</td>
    <td align="right">7</td>
</tr>
<tr bgcolor="#F1E0C6">
    <td>C</td>
    <td align="right">0</td>
    <td align="right">0</td>
    <td align="right">0</td>
    <td align="right">1</td>
</tr>
<tr bgcolor="#D4C0A1">
    <td>D</td>
    <td align="right">0</td>
    <td align="right">0</td>
    <td align="right">0</td>
    <td align="right">7</td>
</tr>
<tr bgcolor="#505050">
    <td class=white><b>Total</b></td>
    <td align="right" class="white"><b>210</b></td>
    <td align="right" class="white"><b>1060458</b></td>
    <td align="right" class="white"><b>1132</b></td>
    <td align="right" class="white"><b>5585115</b></td>
</tr>

我感兴趣的行是带有A,B,C等的行,旁边有数字。

我想出的解决方案是:

table = tree.xpath("//table/tr[td[not(contains(@class, 'white'))]]")

for tr in table:
    print( tr.xpath('td/text()'))

但是,输出仍然包含空单元格的第一行和最后一天/周的输出,如下所示:

['', 'Last Day', 'Last Week']
['A', '0', '3', '0', '13']
['B', '0', '0', '2', '0']
['C', '0', '3', '0', '5']

可以做些什么来摆脱它?

1 个答案:

答案 0 :(得分:1)

只需将tr更改为:

tr[not(contains(@bgcolor, "505050"))]

所以您的代码应如下所示:

from lxml import html

HTML = """<table border="0" cellspacing="1" cellpading="3" width="100%">
<tr bgcolor="#505050">
    <td><b></b></td>
    <td colspan="2" align="center" class="white"><b>Last Day</b></td>
    <td colspan="2" align="center" class="white"><b>Last Week</b></td>
</tr>
<tr bgcolor="#505050">
    <td class="white"><b>Race</b></td>
    <td align="center" class="white"><b>Killed Players</b></td>
    <td align="center" class="white"><b>Killed by Players</b></td>
    <td align="center" class="white"><b>Killed Players</b></td>
    <td align="center" class="white"><b>Killed by Players</b></td>
</tr>
<tr bgcolor="#F1E0C6">
    <td>A</td>
    <td align="right">0</td>
    <td align="right">3</td>
    <td align="right">0</td>
    <td align="right">13</td>
</tr>
<tr bgcolor="#D4C0A1">
    <td>B</td>
    <td align="right">0</td>
    <td align="right">0</td>
    <td align="right">0</td>
    <td align="right">7</td>
</tr>
<tr bgcolor="#F1E0C6">
    <td>C</td>
    <td align="right">0</td>
    <td align="right">0</td>
    <td align="right">0</td>
    <td align="right">1</td>
</tr>
<tr bgcolor="#D4C0A1">
    <td>D</td>
    <td align="right">0</td>
    <td align="right">0</td>
    <td align="right">0</td>
    <td align="right">7</td>
</tr>
<tr bgcolor="#505050">
    <td class=white><b>Total</b></td>
    <td align="right" class="white"><b>210</b></td>
    <td align="right" class="white"><b>1060458</b></td>
    <td align="right" class="white"><b>1132</b></td>
    <td align="right" class="white"><b>5585115</b></td>
</tr>"""

tree = html.fromstring(HTML)
results = defaultdict

for item in tree.xpath('//table/tr[not(contains(@bgcolor, "505050"))]'):
    print item.xpath('.//td/text()')

输出:

['A', '0', '3', '0', '13']
['B', '0', '0', '0', '7']
['C', '0', '0', '0', '1']
['D', '0', '0', '0', '7']

不过,我建议使用 dict() 。参见:

tree = html.fromstring(HTML)
results = dict()

def unpack(data):
    return data[0], data[1:]

for item in tree.xpath('//table/tr[not(contains(@bgcolor, "505050"))]'):
    key, values = unpack(item.xpath('.//td/text()'))
    results[key] = values

print results

输出:

{
    'A': ['0', '3', '0', '13'],
    'C': ['0', '0', '0', '1'],
    'B': ['0', '0', '0', '7'],
    'D': ['0', '0', '0', '7']
}
  

在Python 3中,不需要像上面这样的unpack()函数,你只需要改变    key, values = unpack(item.xpath('.//td/text()'))key, *values = item.xpath('.//td/text()')

请参阅:https://www.python.org/dev/peps/pep-3132/

此外,如果您愿意,可以使用 sorted() 按字母(键)对结果进行排序:

[
  ('A', ['0', '3', '0', '13']),
  ('B', ['0', '0', '0', '7']),
  ('C', ['0', '0', '0', '1']),
  ('D', ['0', '0', '0', '7'])
]