你好,
from bs4 import BeautifulSoup
html = '<p>This <i>is</i> a test.</p>'
soup = BeautifulSoup(html, 'lxml')
print(soup)
for tag in soup.find_all('i'):
tag.replace_with('is')
print(soup)
print("\n")
print(soup.prettify())
print("\n")
for string in soup.stripped_strings:
print(string)
该程序输出以下内容:
<html><body><p>This <i>is</i> a test.</p></body></html>
<html><body><p>This is a test.</p></body></html>
<html>
<body>
<p>
This
is
a test.
</p>
</body>
</html>
This
is
a test
为什么呢?为什么字符串仍然分为三部分,就好像删除的标签仍然存在一样?如果我使用<p>This is a test.</p>(这是我替换标签后的输出)作为我的起始 html,一切都工作正常。
我究竟做错了什么?
提前致谢
守着星空守着你
相关分类