AndrewRM
AndrewRM

Reputation: 43

Using BeautifulSoup how do I remove a single class from an element with multiple classes?

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.

Upvotes: 4

Views: 2905

Answers (2)

salmanwahed
salmanwahed

Reputation: 9647

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.

Upvotes: 3

Cory Kramer
Cory Kramer

Reputation: 117856

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'

Upvotes: 1

Related Questions