我正在尝试使用更改HTML DOM中<p>
标记的内容。我已使用bs4
成功选择特定标记并更新它,但如何将其保存回HTML文件?
Python代码
import bs4
exampleFile = open('stefan/index.html')
exampleSoup = bs4.BeautifulSoup(exampleFile.read())
test="testing testing"
elems = exampleSoup.select('#page_main_text')
print(elems[0].getText())
tag=exampleSoup.p
tag.string=test
print(tag)
HTML
<p class="grey" id="page_main_text">It was an awesome experience to grow up with you. I can recall all the memories that we shared together. We laughed together and cried together. All these things are to remind you – happy birthday.</p>