【发布时间】:2016-10-05 09:12:37
【问题描述】:
假设我有以下 iframe
s=""""
<!DOCTYPE html>
<html>
<body>
<iframe src="http://www.w3schools.com">
<p>Your browser does not support iframes.</p>
</iframe>
</body>
</html>
"""
我想用这个字符串'this is the replacement'替换所有内容 如果我使用
dom = BeatifulSoup(s, 'html.parser')
f = dom.find('iframe')
f.contents[0].replace_with('this is the replacement')
然后我不会替换所有内容,而是只替换第一个字符,在这种情况下是换行符。如果 iframe 完全为空,这也不起作用,因为 f.contents[0] 超出索引
【问题讨论】:
-
要替换
iframe标签中的全部内容吗? -
可以,但不破坏标签
标签: python iframe beautifulsoup