【发布时间】:2010-10-30 23:59:54
【问题描述】:
我有一个 HTML 文件(以 utf-8 编码)。我用codecs.open() 打开它。文件架构为:
<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>
我只需要检索第一个表(丢弃带有表单的表)。省略第一个 <table> 之前和对应的 </table> 之后的所有输入。一些单元格还包含段落、粗体和脚本。主表的每一行嵌套表不超过一个。
如何提取它以获取行列表,其中每个元素包含普通(unicode 字符串)单元格的数据和每个嵌套表的行列表?嵌套层数不超过 1 级。
我尝试了 HTMLParse、PyParse 和 re 模块,但无法正常工作。 我对 Python 很陌生。
【问题讨论】:
-
此问题的 'regex' 标签提出了错误的解决方案 - HTML 语法不规则,正则表达式可能给出错误的结果。
标签: python html html-table extract