how to embed html in an html file using python

You can do such changes using BeautifulSoup as below. You can read more about BeautifulSoup

from bs4 import BeautifulSoup as Soup

with open('<file_path>') as f: 
   soup = Soup(f)

elementDiv = soup.find('div', {"class": "element"}) # find div with class name as element
newDiv = soup.new_tag('div') # adds a new tag
newDiv['id'] = "child"
newDiv.string = "Hello"
elementDiv.append(newDiv) # appends the newDiv within the elementDiv

print(soup)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top