Extract and clean table tags from HTML string
(html_string)
| 54 | |
| 55 | |
| 56 | def extract_table_from_html(html_string): |
| 57 | """Extract and clean table tags from HTML string""" |
| 58 | try: |
| 59 | table_pattern = re.compile(r'<table.*?>.*?</table>', re.DOTALL) |
| 60 | tables = table_pattern.findall(html_string) |
| 61 | tables = [ |
| 62 | re.sub(r'<table[^>]*>', '<table>', table) for table in tables |
| 63 | ] |
| 64 | # tables = [re.sub(r'>\n', '>', table) for table in tables] |
| 65 | return '\n'.join(tables) |
| 66 | except Exception as e: |
| 67 | print(f'extract_table_from_html error: {str(e)}') |
| 68 | return f'<table><tr><td>Error extracting table: {str(e)}</td></tr></table>' |
| 69 | |
| 70 | |
| 71 | rules = [ |