aidputils.agents.tools.http.html_parser

Custom HTML Parser for HTTP Tool.

Provides HTML parsing and CSS selector extraction using Python’s stdlib html.parser, replacing the bs4 (BeautifulSoup4) dependency.

Supports: - Basic CSS selectors: tag, .class, #id, tag.class, tag#id - Text extraction with tag stripping - Element selection for response optimization

class aidputils.agents.tools.http.html_parser.HTMLNode(tag: str, attrs: List[Tuple[str, str | None]] = None)[source]

Bases: object

Represents an HTML element node.

tag
attrs
children: List[HTMLNode]
text: str
parent: HTMLNode | None
get_classes() List[str][source]

Get list of classes from class attribute.

get_id() str | None[source]

Get id attribute value.

matches_selector(selector: str) bool[source]

Check if this node matches a CSS selector.

Supports: - tag: matches tag name - .class: matches class - #id: matches id - tag.class: matches tag with class - tag#id: matches tag with id - .class1.class2: matches multiple classes

get_text(separator: str = ' ', strip: bool = True) str[source]

Extract all text content from this node and descendants.

to_html() str[source]

Convert node back to HTML string.

class aidputils.agents.tools.http.html_parser.SimpleHTMLParser[source]

Bases: HTMLParser

Simple HTML parser that builds a basic DOM tree.

Uses Python’s stdlib html.parser for parsing.

Initialize and reset this instance.

If convert_charrefs is True (the default), all character references are automatically converted to the corresponding Unicode characters.

handle_starttag(tag: str, attrs: List[Tuple[str, str | None]]) None[source]
handle_endtag(tag: str) None[source]
handle_data(data: str) None[source]
get_root() HTMLNode[source]
aidputils.agents.tools.http.html_parser.parse_html(html: str) HTMLNode[source]

Parse HTML string into a node tree.

aidputils.agents.tools.http.html_parser.select_elements(html: str, selector: str) List[HTMLNode][source]

Select elements matching a CSS selector.

Parameters:
  • html – HTML string to parse

  • selector – CSS selector (tag, .class, #id, or combinations)

Returns:

List of matching HTMLNode elements

aidputils.agents.tools.http.html_parser.get_text(html: str, separator: str = ' ', strip: bool = True) str[source]

Extract all text content from HTML, stripping tags.

Parameters:
  • html – HTML string

  • separator – String to join text segments

  • strip – Whether to strip whitespace

Returns:

Plain text content

aidputils.agents.tools.http.html_parser.strip_tags(html: str) str[source]

Remove all HTML tags, returning plain text.

Parameters:

html – HTML string

Returns:

Plain text with tags removed