Fix Python – Can I remove script tags with BeautifulSoup?

Question

Asked By – Sam

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?

Now we will see solution for issue: Can I remove script tags with BeautifulSoup?


Answer

from bs4 import BeautifulSoup
soup = BeautifulSoup('<script>a</script>baba<script>b</script>', 'html.parser')
for s in soup.select('script'):
    s.extract()
print(soup)
baba

This question is answered By – Fábio Diniz

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0