Skip to content

Instantly share code, notes, and snippets.

View abs51295's full-sized avatar
💭
I may be slow to respond.

Aagam Shah abs51295

💭
I may be slow to respond.
View GitHub Profile
@abs51295
abs51295 / html_to_text.py
Last active July 31, 2024 07:07 — forked from racitup/html_to_text.py
Extract text from html in python using BeautifulSoup4
from bs4 import BeautifulSoup, NavigableString, Tag
import urllib.request
def html_to_text(html):
"Creates a formatted text email message as a string from a rendered html template (page)"
soup = BeautifulSoup(html, 'html.parser')
# Ignore anything in head
body, text = soup.body, []
for element in body.descendants:
# We use type and not isinstance since comments, cdata, etc are subclasses that we don't want