2016-10-05 46 views
2
設置爲iFrame

讓說,我有以下的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]超出索引

+0

是否要替換'iframe'標籤的全部內容? – Prabhakar

+0

是的,但不破壞標籤 – LetsPlayYahtzee

回答

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> 
+0

即使替換不只是一個字符串,你是否知道我可以做的任何方式? – LetsPlayYahtzee

+0

@LetsPlayYahtzee你的意思是repl字符串實際上是一個HTML代碼片段? – alecxe

+0

是的,我打開了一個新的[問題](http://stackoverflow.com/questions/39891983/how-to-add-outer-tag-to-beautifulsoup-object)與此有關 – LetsPlayYahtzee

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> 
+0

我正在尋找一種方法,我不會再次創建標籤,您是否知道類似的東西? – LetsPlayYahtzee