from bs4 import BeautifulSoup
with open('index.html', mode='rt', encoding='utf-8') as f:
soup = BeautifulSoup(f.read(), 'html.parser')
print(soup)
nt = soup.new_tag('div', id='hoge') # 新しい要素を作る
nt.string = 'ほげ'
soup.find(class_ = 'footer').append(nt) # UL要素を検索して追加
print(soup)
with open('test.html', mode = 'w', encoding = 'utf-8') as fw:
fw.write(soup.prettify())