I'm looking to remove a single class name from an element that has multiple class names, something like this:
<li class="name1 name2 name3">
<a href="http://www.somelink.com">link</a>
</li>
I can use BeautifulSoup to remove classes in the following way:
soup.find(class_="name3")["class"] = ""
But this removes all classes not only the class that I want to lose.
From your html, you can see,
print soup.find(class_="name3").attrs
{'class': ['name1', 'name2', 'name3']}
So, soup.find(class_="name3")['class']
returns nothing but a list. And you can remove element from it as you can remove elements from list. like,
soup.find(class_="name3")["class"].remove('name1')
This will remove the class that you want to lose.
You could use a generator expression to rebuild the class names you want to keep
s = 'name1 name2 name3'
s = ' '.join(i for i in s.split() if i != 'name3')
>>> s
'name1 name2'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With