[BUG] Web page text splits words at inline tags (un<b>believ</b>able -> 'un believ able') and loses all line breaks
Description
WebPageLoader (behind WebsiteSearchTool and the RAG add() of a URL), ScrapeWebsiteTool and URLReadTool turn HTML into text with soup.get_text(" "). That call puts a space at every tag boundary, inline tags included, so:
- Words that carry markup inside them are split:
un<b>believ</b>ablebecomesun believ able, and<a>docs</a>.becomesdocs .. - The whole page comes back as one line, because no block element or
<br>produces a line break. Paragraphs, list items and headings run together.
The split words then go into the embeddings and into the agent's context. A search for unbelievable or for a linked term followed by punctuation can miss them.
Steps to Reproduce
from bs4 import BeautifulSoup
html = '<h1>Setup</h1><p>That is un<b>believ</b>able, see <a href="/docs">the docs</a>.</p><ul><li>one</li><li>two</li></ul>'
print(repr(BeautifulSoup(html, "html.parser").get_text(" ")))
This is the call in webpage_loader.py, scrape_website_tool.py and url_read_tool.py. After their whitespace clean-up the result is:
'Setup That is un believ able, see the docs . one two'
Expected behavior
Setup
That is unbelievable, see the docs.
one
two
Possible Solution
Join text runs as written, and start a new line at block elements and <br> (table cells separated by a space). One shared helper can serve all three call sites.
crewAI main, beautifulsoup4 4.15.0, Python 3.12, macOS.
Hi, I'd like to take this. #8378 was closed without merging, so I'll open a fresh PR. It will keep words whole across inline tags and put line breaks at block elements and <br> , using a single-pass traversal instead of mutating the tree (to avoid the quadratic cost raised in #8378's review) and handling title/head/body boundaries. I'm using AI assistance (Claude) and will disclose it in the PR.
This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.
This issue was closed because it has been stalled for 5 days with no activity.