Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I want to remove the certain span elements, all up about 10 individual classes. I can do this tediously by repeating my code 10 times, but that is not a correct way to learn.

This is what I got working, that I want to simplify:

for span in table_tag.find_all("span", {'class':'class1'}):

    span.decompose()

for span in table_tag.find_all("span", {'class':'class2'}):

    span.decompose()

for span in table_tag.find_all("span", {'class':'class3'}):

    span.decompose()

This is what I tried but it didn't remove anything:

remove = table_tag.find_all('class1','class2','class3')

for span in table_tag.find_all("span", {'class': remove}):

    span.decompose()

The BeautifulSoup documentation just doesn't hit home with me about how to decompose multiple elements in one command. Also when I go above 5 classes in the find_all command, I get an error that find_all takes from 1 to 6 positional arguments but 7 were given. Obviously I am using the command wrong, just need some help, please.

Thanks in advance.

1 Answer

0 votes
by (36.8k points)

Try this: 

classes = ['class1', 'class2'....]

for i in classes:

    for span in table_tag.find_all("span", {'class': i}):

        span.decompose()

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...