LXML - 解析tr标签

时间:2015-06-14 12:56:39

标签: python html python-3.x lxml

我想从yahoo财务表中解析每个单独的统计信息以进行格式化 - 在解析整个表时,格式化很糟糕!我目前正在使用下面的代码,我将不得不重复4行contentA代码略微改变,以检索表格的每一行内的统计数据。这在下面的contentB变量中举例说明。我拒绝相信这是最有效的方式。有什么建议吗?

from lxml import html   

url = 'http://finance.yahoo.com/q/is?s=MMM+Income+Statement&annual'

tree = html.parse(url)

contentA = tree.xpath("//table[@class='yfnc_tabledata1']/tr[1]/td/table/tr[2]/td[1]")[0].text_content().strip()
contentA1 = tree.xpath("//table[@class='yfnc_tabledata1']/tr[1]/td/table/tr[2]/td[2]")[0].text_content().strip()
contentA2 = tree.xpath("//table[@class='yfnc_tabledata1']/tr[1]/td/table/tr[2]/td[3]")[0].text_content().strip()
contentA3 = tree.xpath("//table[@class='yfnc_tabledata1']/tr[1]/td/table/tr[2]/td[4]")[0].text_content().strip()

contentB = tree.xpath("//table[@class='yfnc_tabledata1']/tr[1]/td/table/tr[3]/td[1]")[0].text_content().strip()
contentB1 = tree.xpath("//table[@class='yfnc_tabledata1']/tr[1]/td/table/tr[3]/td[2]")[0].text_content().strip()
contentB2 = tree.xpath("//table[@class='yfnc_tabledata1']/tr[1]/td/table/tr[3]/td[3]")[0].text_content().strip()
contentG3 = tree.xpath("//table[@class='yfnc_tabledata1']/tr[1]/td/table/tr[3]/td[4]")[0].text_content().strip()

1 个答案:

答案 0 :(得分:2)

使用rangeformat

for i in range(1,5):
    contentA = tree.xpath("//table[@class='yfnc_tabledata1']/tr[1]/td/table/tr[2]/td[{i}]".format(i=i))[0].text_content().strip()
    print(contentA)

输出

Total Revenue
31,821,000
30,871,000
29,904,000
for i in range(1,5):
    contentB = tree.xpath("//table[@class='yfnc_tabledata1']/tr[1]/td/table/tr[3]/td[{i}]".format(i=i))[0].text_content().strip()
    print(contentB)

输出

Cost of Revenue
16,447,000
16,106,000
15,685,000

修改

In [22]: d = {}

In [23]: d.setdefault('Revenue', [])
Out[23]: []

In [24]: for i in range(2,5):
   ....:     contentB = tree.xpath("//table[@class='yfnc_tabledata1']/tr[1]/td/table/tr[3]/td[{i}]".format(i=i))[0].text_content().strip()
   ....:     d['Revenue'].append(int(contentB.replace(',', '')))
   ....:     

In [25]: d
Out[25]: {'Revenue': [16447000, 16106000, 15685000]}