从lxml.etree._Element获取值

时间:2014-07-28 14:50:11

标签: python list python-2.7 xpath lxml

拥有以下内容:

from lxml import html
import io
from lxml import etree

parser = etree.HTMLParser() 
doc   = etree.parse(io.FileIO("index.html"), parser)
i=1

total = []

aa = doc.xpath(".//body/table[10]")
for x in aa:
    for j in x:
        for k in j:
            print type(k) #output is <type 'lxml.etree._Element'>
            print k.text

html文件的一部分:

http://pastebin.com/2eftj9qL

这将显示:

/Request_number_1
15
0
100.00%
1035 ms
923 ms
1407 ms
None


/Request_number_2
15
0
100.00%
966 ms
857 ms
1613 ms
None

如何通过此lxml元素进行迭代?我想检索以毫秒表示的第一个值(例如average_time_array = [1035 ms,966ms])

1 个答案:

答案 0 :(得分:1)

如何使用此xpath表达式? .//body/table[1]/tr/td[5]

average_time_array = [td.text for td in doc.xpath(".//body/table[10]/tr/td[5]")]
print average_time_array