What's the best way to use Python to clean HTML into a text file? or How can I clean HTML into a text

18 Replies, 858 Views

For a no-frills solution, Python’s built-in `html.parser` can work too. Not as fancy, but no extra deps:

```python
from html.parser import HTMLParser

class TextExtractor(HTMLParser):
def __init__(self):
super().__init__()
self.text = []
def handle_data(self, data):
self.text.append(data)

parser = TextExtractor()
parser.feed(open("file.html").read())
print(" ".join(parser.text))
```

Kinda verbose, but gets the job done if you’re avoiding third-party libs.

Messages In This Thread



Users browsing this thread: 1 Guest(s)