Extracting Content from HTML Strings in Python
When working with HTML data in Python, it's often desirable to strip out the formatting tags and retain only the text content. This simplified view of the data can be useful for summarizing text, performing natural language processing, and other tasks.
One way to accomplish this in Python is through the MLStripper class, which utilizes Python's built-in HTML parser.
# For 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()
# For 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()
By passing HTML content to the strip_tags function, you can easily extract only the text portions of the HTML:
cleaned_content = strip_tags("<b>Hello</b> world") # Prints "Hello world"
This MLStripper class and the strip_tags function provide a convenient way to process HTML content in Python, allowing you to focus on the text content without the distractions of formatting tags.
The above is the detailed content of How Can I Efficiently Extract Text Content from HTML Strings in Python?. For more information, please follow other related articles on the PHP Chinese website!