Is it possible to remove all text from HTML nodes with a regex? This very simple case seems to work just fine:
import htmlmin
html = """
<li class="menu-item">
<p class="menu-item__heading">Totopos</p>
<p>Chips and molcajete salsa</p>
<p class="menu-item__details menu-item__details--price">
<strong>
<span class="menu-item__currency"> $ </span>
4
</strong>
</p>
</li>
"""
print(re.sub(">(.*?)<", ">1<", htmlmin.minify(html)))
I tried to use BeautifulSoup but I cannot figure out how to make it work. Using the following code example is not quite correct since it is leaving "4" in as text.
soup = BeautifulSoup(html, "html.parser")
for n in soup.find_all(recursive=True):
print(n.name, n.string)
if n.string:
n.string = ""
print(minify(str(soup)))
2
Answers
try to use
text=True
when you callfind_all
and callextract()
on element to remove it:the output will be in this case:
Attempting to manipulate HTML using regular expressions is almost never the best idea, but this regex should do the trick for you: