English 中文(简体)
如何从超文本中提取表格?
原标题:
  • 时间:2009-06-03 13:48:39
  •  标签:

我有一份超文本文件(载于utf-8)。 开放式)。 档案结构是:

<html>
// header
<body>
  // some text
  <table>
    // some rows with cells here
    // some cells contains tables
  </table>
  // maybe some text here
  <table>
    // a form and other stuff
  </table>
  // probably some more text
</body></html>

我只需要找回头一张表格(用表格打印)。 Omit all submissions before first <table> and subsequently </table>。 一些囚室还载有段落、黑体和文字。 主要表格的每个行数不超过一个表格。

我怎么能抽取一份行文清单,其中每个元件都持有平原(单编码管)电池数据,并列出每一封顶桌的行号? 宽度不超过1级。

I tried HTMLParse, PyParse and re module, but can t get this working. I m quite new to Python.

最佳回答
问题回答

页: 1 我不敢肯定,我确实理解你希望与这一结构做些什么,但也许这个例子会有助于......。

import lxml.html

def process_row(row):
    for cell in row.xpath( ./td ):
       inner_tables = cell.xpath( ./table )
       if len(inner_tables) < 1:
           yield cell.text_content()
       else:
           yield [process_table(t) for t in inner_tables]

def process_table(table):
    return [process_row(row) for row in table.xpath( ./tr )]

html = lxml.html.parse( test.html )
first_table = html.xpath( //body/table[1] )[0]

data = process_table(first_table))




相关问题
热门标签