2012-02-10 53 views
0

這段代碼打印出所有標籤:打印關閉標籤與美麗的湯

soup = BeautifulSoup.BeautifulSoup(html) 
    for child in soup.recursiveChildGenerator(): 
    name = getattr(child, "name", None) 
    if name is not None: 
     print name 

是否可以打印出結束標籤嗎?

例子:

<html> 
    <body> 
     <h1>My First Heading</h1> 
     <p>My first paragraph.</p> 
    </body> 
</html> 

打印:

html 
body 
h1 
p 

我希望它打印:

html 
body 
h1 
/h1 
p 
/p 
/body 
/html 

回答

0

不與機制。您需要手動遞歸,在遞歸嘗試後輸出close標籤。