Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Cleanup from repo

magmueller13bf80d14ed2
diff --git a/src/state_manager/utils.py b/src/state_manager/utils.py
index 0c98d84f3..a581ab117 100644
--- a/src/state_manager/utils.py
+++ b/src/state_manager/utils.py
@@ -1,116 +1,6 @@
-from bs4 import BeautifulSoup
+from bs4 import BeautifulSoup, Tag
import re
import os
-from bs4 import Comment, Tag
-
-
-def cleanup_html(html_content):
- """
- Clean up html content by removing unnecessary HTML elements and formatting.
- Returns a dictionary with cleaned HTML and main content.
- """
- if not html_content:
- return {"html": "", "main_content": ""}
-
- soup = BeautifulSoup(html_content, "html.parser")
- class_counter = 0
-
- # Extract main content before cleaning
- main_content = ""
- main_element = soup.find('main') or soup.find(id='main')
- if main_element:
- main_content = main_element.get_text(strip=True)
- else:
- # If no main element, try to get content from body
- content_containers = soup.select('div[role="main"], div.content, div#content, article, .main-content')
- if content_containers:
- main_content = content_containers[0].get_text(strip=True)
- else:
- # Fallback to body text if no specific content container found
- body = soup.find('body')
- if body:
- main_content = body.get_text(strip=True)
-
- # Remove hidden elements and unnecessary content
- for element in soup.find_all():
- if element is None:
- continue
-
- # Remove if element is hidden
- if hasattr(element, 'attrs') and element.attrs and (
- element.get('aria-hidden') == 'true' or
- element.get('hidden') or
- element.get('style') and ('display: none' in element.get('style') or 'visibility: hidden' in element.get('style')) or
- element.get('class') and ('hidden' in ' '.join(element.get('class')) or
- 'invisible' in ' '.join(element.get('class')))):
- element.decompose()
- continue
-
- # Remove all non-essential elements
- if element.name in ['script', 'style', 'noscript', 'iframe', 'link', 'meta',
- 'head', 'svg', 'path', 'defs', 'clipPath']:
- element.decompose()
- continue
-
- # Find and clean interactive elements
- interactive_selectors = [
- "button", "a", "input", "select", "textarea",
- "[role='button']", "[role='link']", "[role='combobox']",
- "[tabindex='0']", "[aria-haspopup]",
- "[class*='modal']", "[class*='popup']", "[class*='overlay']",
- "[id*='cookie']", "[class*='cookie']", "[aria-label*='cookie']"
- ]
-
- # First pass: mark overlay/popup elements
- overlay_elements = []
- for selector in interactive_selectors[9:]: # Only popup/overlay selectors
- for element in soup.select(selector):
- if element and not element.get('c'):
- element["c"] = str(class_counter)
- class_counter += 1
- overlay_elements.append(element)
-
- # Second pass: mark other interactive elements if no overlays found
- if not overlay_elements:
- for selector in interactive_selectors[:9]: # Non-popup selectors
- for element in soup.select(selector):
- if element is None or element.get('c'):
- continue
-
- # Check if element is visible
- if hasattr(element, 'attrs') and element.attrs and (
- element.get('aria-hidden') == 'true' or
- element.get('hidden') or
- element.get('style') and ('display: none' in element.get('style'))):
- continue
-
- # Add c attribute to interactive element
- element["c"] = str(class_counter)
- class_counter += 1
-
- # Clean up attributes
- for element in soup.find_all():
- if element and element.get('c'):
- # Keep only essential attributes
- allowed_attrs = ["c", "href", "aria-label", "placeholder", "id", "type"]
- attrs = dict(element.attrs)
- for attr in attrs:
- if attr not in allowed_attrs:
- del element[attr]
-
- # Get the HTML string and clean it up
- cleaned_html = str(soup)
- cleaned_html = " ".join(cleaned_html.split())
-
- # Remove empty tags with regex
- empty_tags_pattern = r"<[^/>][^>]*></[^>]+>"
- while re.search(empty_tags_pattern, cleaned_html):
- cleaned_html = re.sub(empty_tags_pattern, "", cleaned_html)
-
- return {
- "html": cleaned_html,
- "main_content": main_content
- }
def save_formatted_html(html_content, output_file_name):
@@ -143,87 +33,117 @@ def save_markdown(markdown_content, output_file_name):
f.write(markdown_content)
-def cleanup_html_old(html_content):
+def cleanup_html(html_content):
"""
- Clean up html content by removing unnecessary HTML elements and formatting.
-
- Args:
- html_content (str): Raw HTML content to clean
-
- Returns:
- str: Cleaned HTML content
+ Clean up HTML content by processing visible and interactive elements.
+ Returns both cleaned HTML and main content.
"""
+ if not html_content:
+ return {"html": "", "main_content": ""}
+
soup = BeautifulSoup(html_content, "html.parser")
+ class_counter = 0
- # Remove comments
- for comment in soup.find_all(string=lambda text: isinstance(text, Comment)):
- comment.extract()
+ def is_interactive_element(element):
+ """Check if element is interactive based on tag or attributes"""
+ interactive_tags = [
+ "a", "button", "input", "select", "textarea", "details",
+ "embed", "label", "menu", "menuitem", "object", "summary"
+ ]
- # Remove unwanted tags completely
- unwanted_tags = ["style", "script", "font", "link", "meta"]
- for tag in unwanted_tags:
- for element in soup.find_all(tag):
- element.decompose()
+ interactive_roles = [
+ "button", "link", "menuitem", "tab", "checkbox", "radio",
+ "combobox", "listbox", "menu", "menuitem", "option", "slider"
+ ]
- # Function to check if a tag only contains another single tag
- def is_wrapper_tag(tag):
- # Check if tag has exactly one child that is a tag
- children = list(tag.children)
- tag_children = [child for child in children if isinstance(child, Tag)]
+ return (
+ element.name in interactive_tags or
+ element.get('role') in interactive_roles or
+ element.get('tabindex') == '0' or
+ element.get('onclick') or
+ element.get('aria-haspopup')
+ )
+ def is_visible(element):
+ """Check if element would be visible"""
+ return not (
+ element.get('aria-hidden') == 'true' or
+ element.get('hidden') or
+ element.get('style') and ('display: none' in element.get('style') or
+ 'visibility: hidden' in element.get('style')) or
+ element.get('class') and ('hidden' in ' '.join(element.get('class')) or
+ 'invisible' in ' '.join(element.get('class')))
+ )
+
+ def is_leaf_element(element):
+ """Check if element is a leaf node with meaningful content"""
return (
- len(tag_children) == 1
- and len(children) == 1
- and not tag.get("id") # Preserve tags with ID
- and not tag.get("class") # Preserve tags with class
- and not tag.string # Ensure there's no direct text content
+ bool(element.string and element.string.strip()) and
+ not element.find_all(True) and
+ element.name not in ['script', 'style', 'meta', 'link', 'svg']
)
- # Remove nested wrapper tags
- modified = True
- while modified:
- modified = False
- # Look for just divs and spans
- for tag in soup.find_all(["div", "span"]):
- if is_wrapper_tag(tag):
- tag.replace_with(tag.contents[0])
- modified = True
-
- # Add counter for class enumeration
- class_counter = 0
+ def process_element(element):
+ """Process a single element and its children"""
+ nonlocal class_counter
- # Remove all attributes except allowed ones
- for tag in soup.find_all(True):
- allowed_attrs = [
- "id",
- "name",
- "aria-label",
- "aria-hidden",
- "href",
- "alt",
- # "class",
- # "data-node-index",
- # "data-p",
- # "class",
- ]
- attrs = dict(tag.attrs)
- for attr in attrs:
- if attr == "class":
- # Replace class with enumerated value
- tag["c"] = str(class_counter)
+ if not isinstance(element, Tag):
+ return
+
+ # Skip hidden elements
+ if not is_visible(element):
+ element.decompose()
+ return
+
+ # Process interactive elements
+ if is_interactive_element(element):
+ if element.get('class'):
+ element['c'] = str(class_counter)
class_counter += 1
- del tag[attr]
- elif attr not in allowed_attrs:
- del tag[attr]
+ del element['class']
- # Get the HTML string
- cleaned_html = str(soup)
+ # Keep only essential attributes
+ allowed_attrs = ["c", "href", "aria-label", "placeholder", "id", "type", "role"]
+ attrs = dict(element.attrs)
+ for attr in attrs:
+ if attr not in allowed_attrs:
+ del element[attr]
+
+ # Process leaf elements with content
+ elif is_leaf_element(element):
+ if element.get('class'):
+ element['c'] = str(class_counter)
+ class_counter += 1
+ del element['class']
+
+ # Remove unnecessary elements
+ elif element.name in ['script', 'style', 'meta', 'link', 'svg', 'iframe']:
+ element.decompose()
+ return
+
+ # Process children recursively
+ for child in element.children:
+ process_element(child)
- # Remove multiple spaces and newlines
+ # Process the entire document
+ process_element(soup.body)
+
+ # Extract main content
+ main_content = ""
+ main_elem = soup.find('main') or soup.find(id='main')
+ if main_elem:
+ main_content = main_elem.get_text(strip=True)
+
+ # Clean up the final HTML
+ cleaned_html = str(soup)
cleaned_html = " ".join(cleaned_html.split())
# Remove empty tags
empty_tags_pattern = r"<[^/>][^>]*></[^>]+>"
- cleaned_html = re.sub(empty_tags_pattern, "", cleaned_html)
+ while re.search(empty_tags_pattern, cleaned_html):
+ cleaned_html = re.sub(empty_tags_pattern, "", cleaned_html)
- return cleaned_html
+ return {
+ "html": cleaned_html,
+ "main_content": main_content
+ }