使用BeautifulSoup将内容添加到iframe

时间:2016-10-05 09:12:37

标签: python iframe beautifulsoup

假设我有以下iframe

 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]超出索引

,这也不起作用

2 个答案:

答案 0 :(得分:2)

只需设置.string property

即可
from bs4 import BeautifulSoup

data = """
 <!DOCTYPE html>
 <html>
 <body>

 <iframe src="http://www.w3schools.com">
   <p>Your browser does not support iframes.</p>
 </iframe>

 </body>
 </html>
"""

soup = BeautifulSoup(data, "html.parser")
frame = soup.iframe

frame.string = 'this is the replacement'

print(soup.prettify())

打印:

<!DOCTYPE html>
<html>
 <body>
  <iframe src="http://www.w3schools.com">
   this is the replacement
  </iframe>
 </body>
</html>

答案 1 :(得分:0)

这将替代iframe代码内容。

s="""
 <!DOCTYPE html>
 <html>
 <body>
 <iframe src="http://www.w3schools.com">
   <p>Your browser does not support iframes.</p>
 </iframe>
 </body>
 </html>
 """
from BeautifulSoup import BeautifulSoup
from HTMLParser import HTMLParser

soup = BeautifulSoup(s, convertEntities=BeautifulSoup.HTML_ENTITIES)
show= soup.findAll('iframe')[0]
show.replaceWith('<iframe src="http://www.w3schools.com">this is the replacement</iframe>'.encode('utf-8'))
html = HTMLParser()
print html.unescape(str(soup.prettify()))

输出:

<!DOCTYPE html>
<html>
 <body>
  <iframe src="http://www.w3schools.com">my text</iframe>
 </body>
</html>