This file contains 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 | |
# 20190720, from: https://stackoverflow.com/questions/30565404/remove-all-style-scripts-and-html-tags-from-an-html-pagesoup = BeautifulSoup(str_html) | |
for s in soup(["head"]): | |
s.decompose() | |
cleaned_html = str(soup) |
This file contains 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 lxml.html.clean import Cleaner | |
#to prevent Cleaner to replace html with div, leave page_structure alone: http://stackoverflow.com/questions/15556391/lxml-clean-html-replaces-html-tag-with-div | |
cleaner = Cleaner(page_structure=False) | |
#according to: http://stackoverflow.com/questions/8554035/remove-all-javascript-tags-and-style-tags-from-html-with-python-and-the-lxml-mod | |
#Cleaner is a better general solution to the problem than using strip_elements, because in cases like this you want to strip out more than just the <script> tag; you also want to get rid of things like onclick=function() attributes on other tags. | |
cleaner.javascript=True | |
cleaner.scripts=True | |
#turn this on in the future if necessary | |
#cleaner.style=True |