Python Forum

Full Version: How to clean html content using BeautifulSoup in Python 3.6?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have created a pandas DataFrame which stores the html content of a product description. The html content is like below-

<p><img src="//ad.xyz.com/s/files/1/2352/2977/files/logo-3_large.png?v=1512189111" alt="10mois 5 in 1 Convertible Baby Bed &amp; Desk"><br><br></p>\n<h1><strong>10 mois 5 in 1 Convertible Baby Bed &amp; Desk<br><br></strong></h1>

Now I need to write a function which can parse the html tags using BeautifulSoup and can return a filtered version with whitelisted tags only.

Here whitelisted tags is basically a list of desired tags as below-
whitelist = ['p', 'h1','b','i','u','br','li']

Can anyone please help me to achieve this using Python 3.6?

Thanks!
Please, post your code in code tags, any traceback in error tags and ask specific questions.
I continue to not understand why you put html in pandas data frame. For me normal/natural approach would be to request the page, parse the html with BeautifulSoup and extract desired data and only then if you are going to process data, put them in a dataframe.
Hi Buran,

I am getting a response from an e-commerce site in the form of json and this response contains many attributes in which html content is in 'body_html' attribute.
So after getting the response I amd storing only the html content in a dataframe.
If there is an alternate approach please suggest me that also.

I am trying with below code-
product_description = data["body_html"]
def filter_product_description(product_description):
	whitelist = ['p', 'h1','b','strong','span']
    html_series = product_description.all()
    # print(html_series)
    keep = []
    for html_description in html_series:

        soup = BeautifulSoup(html_description, "html.parser")

        for tag in soup.findAll(True):
            if tag in whitelist:
                keep.append(tag)

    return keep
res= filter_product_description(product_description)
print(res)
I want to use this function as cleaning up of html content which returns inly the text which have the tags listed in whitelist.

Thanks!
from bs4 import BeautifulSoup

html_data = '''\
<html>
<h2>No me</h2>
<p><img src="url" alt="Baby Bed &amp; Desk"><br><br></p>
<h1><strong>Convertible Baby Bed &amp; Desk<br><br></strong></h1>
<footer> Not me </footer>
</html>'''

soup = BeautifulSoup(html_data, 'lxml')
Test:
>>> whitelist = ['p', 'h1', 'b', 'i', 'u','br','li']
>>> clean = [tag for tag in soup.find_all() if tag.name in whitelist]
>>> clean
[<p><img alt="Baby Bed &amp; Desk" src="url"/><br/><br/></p>,
 <br/>,
 <br/>,
 <h1><strong>Convertible Baby Bed &amp; Desk<br/><br/></strong></h1>,
 <br/>,
 <br/>]

>>> clean = set(clean)
>>> clean
{<p><img alt="Baby Bed &amp; Desk" src="url"/><br/><br/></p>,
 <h1><strong>Convertible Baby Bed &amp; Desk<br/><br/></strong></h1>,
 <br/>}

>>> list(clean)[:-1]
[<p><img alt="Baby Bed &amp; Desk" src="url"/><br/><br/></p>,
 <h1><strong>Convertible Baby Bed &amp; Desk<br/><br/></strong></h1>]
Then have p and h1 back,inner tag inside p will still be there.
what if I don't want include <img>, <iframe> tags?
(Apr-27-2018, 07:05 AM)PrateekG Wrote: [ -> ]what if I don't want include <img>, <iframe> tags?
The have to do second cleaning,to clean tags inside other tag.
Now it start to get complex,usually this is the other way around.
Which mean that you parse date you do want,an not like now try filter out all data that's not wanted Doh