Created
April 7, 2020 11:45
-
-
Save morrisalp/043a6d22536b7e8c7449748e43ff8ba4 to your computer and use it in GitHub Desktop.
sane text extraction given html string, using BeautifulSoup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bs4 import BeautifulSoup as bs | |
def html2text(html): | |
soup = bs(html, features='lxml') | |
for script in soup(["script", "style"]): | |
script.decompose() | |
for br in soup.find_all("br"): | |
br.replace_with("\n") | |
return soup.get_text(separator=' ').strip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment