Tags¤
A set of functions to replace or remove HTML/XML tags using Beautiful Soup.
_compile_regex(pattern: str) -> re.Pattern[str]
cached
¤
_get_parser(mode: str) -> str
¤
Return the appropriate BeautifulSoup parser for the given mode.
Source code in lexos/scrubber/tags.py
_parse_document(text: str, mode: str) -> BeautifulSoup
¤
Parse HTML or XML text using the appropriate parser.
For XML mode, BeautifulSoup prefers 'lxml-xml' when available. If parser initialization fails, this function falls back to the built-in 'xml' parser before raising the original exception.
Source code in lexos/scrubber/tags.py
_filter_elements_by_attribute(elements: list, attribute_name: Optional[str], attribute_value: Optional[str], matcher_type: str) -> list
¤
Filter a list of elements by an attribute name and optional value.
Source code in lexos/scrubber/tags.py
_match_elements(selector: Optional[str], text: str, mode: str = 'html', matcher_type: Optional[str] = 'exact', attribute: Optional[str | list[str]] = None, attribute_value: Optional[str] = None, attribute_filter: Optional[str] = None) -> tuple[BeautifulSoup, list]
¤
Finds HTML/XML elements matching a selector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Optional[str]
|
Tag name or CSS selector to match elements |
required |
text
|
str
|
HTML or XML text to process |
required |
mode
|
str
|
Parser mode, either "html" or "xml" |
'html'
|
matcher_type
|
Optional[str]
|
Type of match to perform, either "exact", "contains", or "regex" |
'exact'
|
attribute
|
Optional[str | list[str]]
|
Optional attribute name to filter elements |
None
|
attribute_value
|
Optional[str]
|
Optional value for the attribute filter |
None
|
attribute_filter
|
Optional[str]
|
Optional attribute name to filter elements |
None
|
Returns:
| Type | Description |
|---|---|
tuple[BeautifulSoup, list]
|
A BeautifulSoup object and a list of matching elements. |
Source code in lexos/scrubber/tags.py
_compute_replacement_value(old_attribute_value: str | list[str], matcher_type: str, attribute_value: Optional[str], replace_value: Optional[str | list[str]]) -> str
¤
Source code in lexos/scrubber/tags.py
_match_value(attribute_value: str | list[str], pattern: str, match_type: str = 'exact') -> bool
¤
Match attribute values using different matching strategies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attribute_value
|
str | list[str]
|
The attribute value(s) to match against (string or list) |
required |
pattern
|
str
|
The pattern/string to match |
required |
match_type
|
str
|
Type of matching - "exact", "contains", or "regex" |
'exact'
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the pattern matches according to the specified type, False otherwise. |
Raises:
| Type | Description |
|---|---|
LexosException
|
If match_type is not one of the valid options. |
Source code in lexos/scrubber/tags.py
remove_attribute(text: str, selector: str, attribute: str = None, mode: str = 'html', matcher_type: str = 'exact', attribute_value: Optional[str] = None, attribute_filter: Optional[str] = None) -> str
¤
Removes attributes from HTML/XML elements.
Removes specified attributes from elements matching the selector. Can filter elements by specific attribute or attribute value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
HTML or XML text to process |
required |
selector
|
str
|
Tag name or CSS selector to match elements |
required |
attribute
|
str
|
Attribute name to remove. |
None
|
mode
|
str
|
Parser mode, either "html" or "xml" |
'html'
|
matcher_type
|
str
|
Type of match to perform, either "exact", "contains", or "regex" |
'exact'
|
attribute_value
|
Optional[str]
|
Optional value for the attribute filter |
None
|
attribute_filter
|
Optional[str]
|
Optional attribute name to filter elements |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Processed text with attributes removed from matching elements |
Raises:
| Type | Description |
|---|---|
LexosException
|
If mode is not "html" or "xml" |
Examples:
>>> text = '<div class="main" id="content">Text</div>'
>>> remove_attributes(text, "div", "class")
'<div id="content">Text</div>'
>>> text = '<p class="a">Keep</p><p class="b" id="x">Remove attrs</p>'
>>> remove_attributes(text, "p", attribute_filter="class", attribute_value="b")
'<p class="a">Keep</p><p>Remove attrs</p>'
Source code in lexos/scrubber/tags.py
remove_comments(text: str, mode: str = 'html') -> str
¤
Removes comments from HTML or XML text.
Uses BeautifulSoup to find and remove all comments from HTML or XML content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
HTML or XML text to process |
required |
mode
|
str
|
Parser mode, either "html" or "xml" |
'html'
|
Returns:
| Type | Description |
|---|---|
str
|
String containing the HTML/XML content with all comments removed |
Raises:
| Type | Description |
|---|---|
LexosException
|
If mode is not "html" or "xml" |
Examples:
>>> html = '<!-- Header comment --><div>Content</div><!-- Footer -->'
>>> remove_comments(html)
'<div>Content</div>'
>>> xml = '<?xml version="1.0"?><!-- Config --><root>Data</root>'
>>> remove_comments(xml, mode="xml")
'<?xml version="1.0"?><root>Data</root>'
Source code in lexos/scrubber/tags.py
remove_doctype(text: str) -> str
¤
Removes a document type declaration from HTML or XML text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
HTML or XML text to process |
required |
Returns:
| Type | Description |
|---|---|
str
|
String containing the HTML/XML content with document type declaration removed |
Source code in lexos/scrubber/tags.py
remove_element(text: str, selector: str, mode: str = 'html', matcher_type: str = 'exact', attribute: str = None, attribute_value: str = None) -> str
¤
Removes HTML/XML elements using BeautifulSoup.
Removes elements that match the given selector from HTML or XML text. Can further filter elements by specific attribute or attribute value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
HTML or XML text to process |
required |
selector
|
str
|
Tag name or CSS selector to match elements for removal |
required |
mode
|
str
|
Parser mode, either "html" or "xml" |
'html'
|
matcher_type
|
str
|
Type of match to perform, either "exact", "contains", or "regex" |
'exact'
|
attribute
|
str
|
Optional attribute name to filter elements |
None
|
attribute_value
|
str
|
Optional value for the attribute filter |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Processed text with matching elements removed |
Raises:
| Type | Description |
|---|---|
LexosException
|
If mode is not "html" or "xml" |
Examples:
>>> text = "<p class='a'>Keep</p><p class='b'>Remove</p><div>Remove</div>"
>>> remove_element(text, "div")
'<p>Keep</p>'
>>> remove_element("text", "p", attribute="class", attribute_value="b")
"<p class='a'>Keep</p><div>Remove</div>"
Source code in lexos/scrubber/tags.py
remove_tag(text: str, selector: str, mode: str = 'html', matcher_type: str = 'exact', attribute: str = None, attribute_value: str = None) -> str
¤
Removes HTML/XML tags but keeps their inner content.
Removes tags matching the selector while preserving their inner content. Can filter elements by specific attribute or attribute value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
HTML or XML text to process |
required |
selector
|
str
|
Tag name or CSS selector to match elements for unwrapping |
required |
mode
|
str
|
Parser mode, either "html" or "xml" |
'html'
|
matcher_type
|
str
|
Type of match to perform, either "exact", "contains", or "regex" |
'exact'
|
attribute
|
str
|
Optional attribute name to filter elements |
None
|
attribute_value
|
str
|
Optional value for the attribute filter |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Processed text with matching tags unwrapped but content preserved |
Raises:
| Type | Description |
|---|---|
LexosException
|
If mode is not "html" or "xml" |
Examples:
>>> text = "<div><p>Keep this</p></div><span>And this</span>"
>>> remove_tag(text, "div")
'<p>Keep this</p><span>And this</span>'
>>> text = "<p class='a'>Keep tag</p><p class='b'>Remove tag only</p>"
>>> remove_tag(text, "p", attribute="class", attribute_value="b")
"<p class='a'>Keep tag</p>Remove tag only"
Source code in lexos/scrubber/tags.py
replace_attribute(text: str, selector: str, old_attribute: str, new_attribute: str, mode: str = 'html', matcher_type: str = 'exact', attribute_value: Optional[str] = None, replace_value: Optional[str] = None, attribute_filter: Optional[str] = None, filter_value: Optional[str] = None) -> str
¤
Replaces HTML/XML element attributes or their values.
This function finds elements matching the selector and replaces attribute names or attribute values. It can filter elements by a specific attribute/value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
HTML or XML text to process |
required |
selector
|
str
|
Tag name or CSS selector to match elements |
required |
old_attribute
|
str
|
Name of the attribute to replace |
required |
new_attribute
|
str
|
Name of the new attribute (or same name if only changing value) |
required |
mode
|
str
|
Parser mode, either "html" or "xml" |
'html'
|
matcher_type
|
str
|
Type of match to perform, either "exact", "contains", or "regex" |
'exact'
|
attribute_value
|
Optional[str]
|
Only replace attributes with this specific value |
None
|
replace_value
|
Optional[str]
|
New value to use (keeps original value if None) |
None
|
attribute_filter
|
Optional[str]
|
Optional attribute name to filter elements |
None
|
filter_value
|
Optional[str]
|
Optional value for the attribute filter |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Processed text with attributes replaced in matching elements |
Raises:
| Type | Description |
|---|---|
LexosException
|
If mode is not "html" or "xml" |
Examples:
>>> # Replace class attribute with data-type, keeping the value
>>> text = '<div class="main">Text</div>'
>>> replace_attribute(text, "div", "class", "data-type")
'<div data-type="main">Text</div>'
>>> # Replace class="info" with class="highlight"
>>> text = '<p class="info">Text</p><p class="data">More</p>'
>>> replace_attribute(text, "p", "class", "class", filter_value="info", replace_value="highlight")
'<p class="highlight">Text</p><p class="data">More</p>'
>>> # Only replace attributes on elements with a specific attribute value
>>> text = '<div class="main" id="content">Text</div><div class="sidebar">Side</div>'
>>> replace_attribute(text, "div", "class", "role", attribute_filter="id", filter_value="content")
'<div role="main" id="content">Text</div><div class="sidebar">Side</div>'
Source code in lexos/scrubber/tags.py
replace_tag(text: str, selector: str, replacement: str, mode: str = 'html', matcher_type: str = 'exact', attribute: str = None, attribute_value: str = None, preserve_attributes: bool = True) -> str
¤
Replaces HTML/XML tags with another tag while preserving content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
HTML or XML text to process |
required |
selector
|
str
|
Tag name or CSS selector to match elements for replacement |
required |
replacement
|
str
|
New tag name to replace the matched elements with |
required |
mode
|
str
|
Parser mode, either "html" or "xml" |
'html'
|
matcher_type
|
str
|
Type of match to perform, either "exact", "contains", or "regex" |
'exact'
|
attribute
|
str
|
Optional attribute name to filter elements |
None
|
attribute_value
|
str
|
Optional value for the attribute filter |
None
|
preserve_attributes
|
bool
|
Whether to preserve original tag attributes |
True
|
Returns:
| Type | Description |
|---|---|
str
|
Processed text with matching tags replaced but content preserved |
Raises:
| Type | Description |
|---|---|
LexosException
|
If mode is not "html" or "xml" |
Examples:
>>> text = "<div><p>Keep this</p></div>"
>>> replace_tag(text, "div", "section")
'<section><p>Keep this</p></section>'
>>> text = "<p class='a'>Keep</p><p class='b' id='x'>Replace tag</p>"
>>> replace_tag(text, "p", "span", attribute="class", attribute_value="b")
"<p class='a'>Keep</p><span class='b' id='x'>Replace tag</span>"