Can I remove script tags with BeautifulSoup?

Sam picture Sam · Apr 8, 2011 · Viewed 95.7k times · Source

Can script tags and all of their contents be removed from HTML with BeautifulSoup, or do I have to use Regular Expressions or something else?

Answer

Fábio Diniz picture Fábio Diniz · Apr 8, 2011
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('<script>a</script>baba<script>b</script>', 'lxml')
>>> for s in soup.select('script'):
>>>    s.extract()
>>> soup
baba