How do I strip tags from an HTML document using lxml?

To remove tags from an HTML document using lxml, you can make use of the etree module within lxml. The process typically involves parsing the HTML content, iterating over elements, and then either removing or replacing the tags while preserving the text content.

Here's a Python function that demonstrates how to strip tags using lxml:

from lxml import etree, html

def strip_tags(html_content):
    # Parse the HTML content
    parser = html.HTMLParser(remove_comments=True)
    document = html.fromstring(html_content, parser=parser)

    # Use XPath to select all elements and iterate through them
    for element in document.xpath('.//*'):
        # If the element is not the root element
        if element is not document:
            # Replace the element with its text content
            element.drop_tag()

    # Return the string representation of the modified HTML
    return etree.tostring(document, encoding='unicode')

# Example HTML content
html_data = '''
<html>
    <body>
        <p>This is a <a href="http://example.com">link</a>.</p>
        <div>And this is a <span style="color: red;">red text</span>.</div>
    </body>
</html>
'''

# Stripping the tags
cleaned_html = strip_tags(html_data)

print(cleaned_html)

The strip_tags function above parses the provided HTML content into an element tree, then iterates through all elements in the tree. For each element, the drop_tag() method is called, which removes the tag but keeps its content and tail text intact.

This will produce an output where the tags are removed, but the text content is preserved:

This is a link.
And this is a red text.

Keep in mind that this example removes all HTML tags except for the root element. If you want to remove only specific tags, you'll need to adjust the XPath expression or add conditional logic inside the loop.

Please note that lxml is a powerful library that requires correct handling to avoid potential issues such as broken HTML structures. When using strip_tags, make sure that the resulting text makes sense in the context of your application, as some HTML formatting and semantics will be lost when tags are stripped.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon