简单的 HTML 表格解析(不依赖 BeautifulSoup)。 仅用于备用,可能不够健壮。
(html_content: str)
| 1072 | |
| 1073 | |
| 1074 | def _parse_html_table_simple(html_content: str) -> tuple[List[str], List[List[str]]]: |
| 1075 | """ |
| 1076 | 简单的 HTML 表格解析(不依赖 BeautifulSoup)。 |
| 1077 | 仅用于备用,可能不够健壮。 |
| 1078 | """ |
| 1079 | try: |
| 1080 | # 简单正则提取所有 <tr>...</tr> 内容 |
| 1081 | import re |
| 1082 | tr_pattern = re.compile(r'<tr>(.*?)</tr>', re.DOTALL | re.IGNORECASE) |
| 1083 | td_pattern = re.compile(r'<t[dh][^>]*>(.*?)</t[dh]>', re.DOTALL | re.IGNORECASE) |
| 1084 | |
| 1085 | rows_data = [] |
| 1086 | for tr_match in tr_pattern.finditer(html_content): |
| 1087 | tr_content = tr_match.group(1) |
| 1088 | row = [] |
| 1089 | for td_match in td_pattern.finditer(tr_content): |
| 1090 | cell_text = td_match.group(1).strip() |
| 1091 | # 移除 HTML 标签 |
| 1092 | cell_text = re.sub(r'<[^>]+>', '', cell_text) |
| 1093 | row.append(cell_text) |
| 1094 | if row: |
| 1095 | rows_data.append(row) |
| 1096 | |
| 1097 | if not rows_data: |
| 1098 | return [], [] |
| 1099 | |
| 1100 | headers = rows_data[0] |
| 1101 | data_rows = rows_data[1:] if len(rows_data) > 1 else [] |
| 1102 | |
| 1103 | return headers, data_rows |
| 1104 | |
| 1105 | except Exception as e: |
| 1106 | log.error(f"[_parse_html_table_simple] 简单解析失败: {e}") |
| 1107 | return [], [] |
| 1108 | |
| 1109 | |
| 1110 | def extract_tables_from_mineru_results( |