使用BeautifulSoup中的replace_with()方法替换标签内容,这将导致soup对象被更新。以下是示例代码:
from bs4 import BeautifulSoup
html = "
div_tag = soup.find('div') div_tag.string = "new content" print(soup) # 输出:
new_content = "new content 2" div_tag.replace_with(new_content) print(soup) # 输出:
new content 2,soup对象被更新