| 206 | print({'simple': (num_simple,avg_score_simple), 'complex': (num_complex,avg_score_complex), 'total': (num_total,avg_score)}) |
| 207 | |
| 208 | def simplify_html_table(html_table): |
| 209 | # 使用 BeautifulSoup 解析 HTML |
| 210 | soup = BeautifulSoup(html_table, 'html.parser') |
| 211 | |
| 212 | # 找到 <table> 标签 |
| 213 | table = soup.find('table') |
| 214 | if not table: |
| 215 | raise ValueError("输入的 HTML 不包含有效的 <table> 标签") |
| 216 | |
| 217 | # 创建一个新的 <table> 标签 |
| 218 | new_table = BeautifulSoup('<table></table>', 'html.parser').table |
| 219 | |
| 220 | # 提取所有行(包括 <thead> 和 <tbody> 中的行) |
| 221 | rows = table.find_all(['tr'], recursive=True) |
| 222 | |
| 223 | for row in rows: |
| 224 | # 创建新的 <tr> 标签 |
| 225 | new_row = soup.new_tag('tr') |
| 226 | |
| 227 | # 处理每一行中的单元格 |
| 228 | cells = row.find_all(['th', 'td']) |
| 229 | for cell in cells: |
| 230 | # 将 <th> 替换为 <td> |
| 231 | new_cell = soup.new_tag('td') |
| 232 | if cell.has_attr('rowspan'): |
| 233 | new_cell['rowspan'] = cell['rowspan'] |
| 234 | if cell.has_attr('colspan'): |
| 235 | new_cell['colspan'] = cell['colspan'] |
| 236 | new_cell.string = cell.get_text(strip=True) # 保留单元格内容 |
| 237 | new_row.append(new_cell) |
| 238 | |
| 239 | # 将新行添加到新表格中 |
| 240 | new_table.append(new_row) |
| 241 | |
| 242 | # 返回简化后的表格 HTML |
| 243 | return str(new_table) |
| 244 | |
| 245 | |
| 246 | def main(): |