讓說,我有以下的iframe添加內容與BeautifulSoup
s=""""
<!DOCTYPE html>
<html>
<body>
<iframe src="http://www.w3schools.com">
<p>Your browser does not support iframes.</p>
</iframe>
</body>
</html>
"""
我想替換這個字符串「這是替換」 如果我使用
dom = BeatifulSoup(s, 'html.parser')
f = dom.find('iframe')
f.contents[0].replace_with('this is the replacement')
所有內容,然後而不是取代所有的內容,我只會替換第一個字符,在這種情況下是換行符。此外,如果iframe完全爲空是因爲f.contents [0]超出索引
是否要替換'iframe'標籤的全部內容? – Prabhakar
是的,但不破壞標籤 – LetsPlayYahtzee