Can I remove script tags with BeautifulSoup?

Question:

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?

Asked By: Sam

||

Answers:

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
Answered By: Fábio Diniz

As stated in the (official documentation) you can use the extract method to remove all the subtree that matches the search.

import BeautifulSoup
a = BeautifulSoup.BeautifulSoup("<html><body><script>aaa</script></body></html>")
[x.extract() for x in a.findAll('script')]
Answered By: Santiago Alessandri

Updated answer for those who might need for future reference:
The correct answer is.
decompose().
You can use different ways but decompose works in place.

Example usage:

soup = BeautifulSoup('<p>This is a slimy text and <i> I am slimer</i></p>')
soup.i.decompose()
print str(soup)
#prints '<p>This is a slimy text and</p>'

Pretty useful to get rid of detritus like <script>, <img> and so forth.

Answered By: Abhishek Dujari
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.