如何使用bs4 python更新本地html页面中特定标记的内容?

时间:2016-07-19 09:05:26

标签: python bs4

我正在尝试使用更改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>

0 个答案:

没有答案