Skip to content

Official Crawl4AI Cloud: a hosted web scraping, extraction, and search API for AI agents. Free API key, 1,000 credits a month, no card. Fully open source: this repo is the Apache-2.0 crawler that powers it, and you can self-host it.

fix(html2text): preserve all attributes on table tags when bypass_tables is enabled (#2007)

ntohidi511c73c7fd37
diff --git a/crawl4ai/html2text/__init__.py b/crawl4ai/html2text/__init__.py
index efbe2701..ff39fb21 100644
--- a/crawl4ai/html2text/__init__.py
+++ b/crawl4ai/html2text/__init__.py
@@ -701,14 +701,19 @@ class HTML2Text(html.parser.HTMLParser):
elif self.bypass_tables:
if start:
self.soft_br()
- if tag in ["td", "th"]:
- if start:
- self.o("<{}>\n\n".format(tag))
+ attr_str = ""
+ if attrs:
+ attr_str = "".join(
+ ' {}="{}"'.format(k, v) if v is not None else " {}".format(k)
+ for k, v in attrs.items()
+ )
+ if tag in ["td", "th"]:
+ self.o("<{}{}>\n\n".format(tag, attr_str))
else:
- self.o("\n</{}>".format(tag))
+ self.o("<{}{}>".format(tag, attr_str))
else:
- if start:
- self.o("<{}>".format(tag))
+ if tag in ["td", "th"]:
+ self.o("\n</{}>".format(tag))
else:
self.o("</{}>".format(tag))