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, 864 Views

If you’re dealing with super messy HTML, try `bleach` + BeautifulSoup. Bleach cleans the HTML first, then BS4 extracts text.

```python
import bleach
from bs4 import BeautifulSoup

clean = bleach.clean(open("file.html").read())
soup = BeautifulSoup(clean, "html.parser")
print(soup.get_text())
```

Extra step, but worth it for nasty files when you need python clean html into text file reliably.

Messages In This Thread



Users browsing this thread: 1 Guest(s)