pythonbeautifulsoup

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.


Solution

  • 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.