"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Python efficient way to remove HTML tags from text

Python efficient way to remove HTML tags from text

Posted on 2025-05-01
Browse:653

How Can I Efficiently Strip HTML Tags from Text in Python?

Stripping HTML Tags in Python for a Pristine Textual Representation

Manipulating HTML responses often involves extracting relevant text content while eliminating the formatting tags. This can be achieved by effectively stripping HTML tags, leaving you with the desired plain text.

Achieving Text-Only Extraction with Python's MLStripper

To streamline the stripping process, the Python standard library provides an efficient function, MLStripper, designed specifically for this purpose. MLStripper takes HTML input and parses it, preserving only non-markup content.

Implementation for Python 3 and 2

Depending on your Python version, you can utilize the following code snippets:

Python 3:

from io import StringIO
from html.parser import HTMLParser

class MLStripper(HTMLParser):
    def __init__(self):
        super().__init__()
        self.reset()
        self.strict = False
        self.convert_charrefs= True
        self.text = StringIO()
    def handle_data(self, d):
        self.text.write(d)
    def get_data(self):
        return self.text.getvalue()

def strip_tags(html):
    s = MLStripper()
    s.feed(html)
    return s.get_data()

Python 2:

from HTMLParser import HTMLParser
from StringIO import StringIO

class MLStripper(HTMLParser):
    def __init__(self):
        self.reset()
        self.text = StringIO()
    def handle_data(self, d):
        self.text.write(d)
    def get_data(self):
        return self.text.getvalue()

def strip_tags(html):
    s = MLStripper()
    s.feed(html)
    return s.get_data()

Usage:

Simply call the strip_tags function passing the HTML input as a string argument. The returned value will be a stripped string with all HTML tags removed.

This technique proves invaluable when you need to work with textual data extracted from HTML sources, ensuring a clean and manageable text representation.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3