I'm modifying a HTML file using Python and BeautifulSoup, and I can change the content of headers, but I couldn't find a way to change the class of a div. My goal is to turn:
<div id="div1" class="blue_titles">test</div>
Into:
<div id="div1" class="green_titles">test</div>
I looked up and down the docs, but to no avail. It's probably right in my face, but I can't find it.
You can simply assign the new value to the key class
:
from bs4 import BeautifulSoup
soup = BeautifulSoup("""<div id="div1" class="blue_titles">test</div>""", "lxml")
soup.find("div")['class'] = "green_titles"
soup
# <html><body><div class="green_titles" id="div1">test</div></body></html>