Remove all style, scripts, and HTML tags using BeautifulSoup - GeeksforGeeks

25 Feb, 2021
Prerequisite: BeautifulSoup, Requests
Beautiful Soap is a Python library for pulling data out of HTML and XML files. In this article, we are going to discuss how to remove all style, scripts, and HTML tags using beautiful soap.
Required Modules:
bs4: Beautiful Soup (bs4) is a python library primarily used to extract data from HTML, XML, and other markup languages. It’s one of the most used libraries for Web Scraping. Run the following command in the terminal to install this library-
pip install bs4
requests: This library is used for making HTTP requests in python.Run the following command in the terminal to install this library-
pip install requests
Approach:
Import bs4 library
Create an HTML doc
Parse the content into a BeautifulSoup object
Iterate over the data to remove the tags from the document using decompose() method
Use stripped_strings() method to retrieve the tag content
Print the extracted data
Implementation:
Python3
# Import Modulefrom bs4 import BeautifulSoup  # HTML DocumentHTML_DOC = """              <html>                <head>                    <title> Geeksforgeeks </title>                    <style>.call {background-color:black;} </style>                    <script>getit</script>                </head>                <body>                    is a                    <div>Computer Science portal.</div>                </body>              </html>            """  # Function to remove tagsdef remove_tags(html):      # parse html content    soup = BeautifulSoup(html, "html.parser")      for data in soup(['style', 'script']):        # Remove tags        data.decompose()      # return data by retrieving the tag content    return ' '.join(soup.stripped_strings)    # Print the extracted dataprint(remove_tags(HTML_DOC))
Output:
Geeksforgeeks is a Computer Science portal.
Approach:
Import bs4 and requests library
Get content from the given URL using requests instance
Parse the content into a BeautifulSoup object
Iterate over the data to remove the tags from the document using decompose() method
Use stripped_strings() method to retrieve the tag content
Print the extracted data
Implementation:
Python3
# Import Modulefrom bs4 import BeautifulSoupimport requests  # Website URLURL = 'https://www.geeksforgeeks.org/data-structures/'  # Page content from Website URLpage = requests.get(URL)  # Function to remove tagsdef remove_tags(html):      # parse html content    soup = BeautifulSoup(html, "html.parser")      for data in soup(['style', 'script']):        # Remove tags        data.decompose()      # return data by retrieving the tag content    return ' '.join(soup.stripped_strings)    # Print the extracted dataprint(remove_tags(page.content))
Output:
Picked
Python BeautifulSoup
Python bs4-Exercises
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()
Reading and Writing to text files in Python
*args and **kwargs in Python
Create a Pandas DataFrame from Lists