Beautiful Soup 简明教程
Beautiful Soup - unwrap() Method
Method Description
unwrap() 方法与 wrap() 方法相反。它用标签内部的任何内容替换该标签。它从元素中移除标签并返回它。
The unwrap() method is the opposite of wrap() method. It It replaces a tag with whatever’s inside that tag. It removes the tag from an element and returns it.
Example 1
在以下示例中,从 HTML 字符串中移除 <b> 标签。
In the following example, the <b> tag from the html string is removed.
html = '''
<p>The quick, <b>brown</b> fox jumps over a lazy dog.</p>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, "html.parser")
tag1 = soup.find('b')
newtag = tag1.unwrap()
print (soup)
Example 2
下面的代码打印 unwrap() 方法的返回的值。
The code below prints the returned value of unwrap() method.
html = '''
<p>The quick, <b>brown</b> fox jumps over a lazy dog.</p>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, "html.parser")
tag1 = soup.find('b')
newtag = tag1.unwrap()
print (newtag)
Example 3
unwrap() 方法非常适合剥离标记,如下代码所示:
The unwrap() method is useful for good for stripping out markup, as the following code shows −
html = '''
<html>
<body>
<p>The quick, brown fox jumps over a lazy dog.</p>
<p>DJs flock by when MTV ax quiz prog.</p>
<p>Junk MTV quiz graced by fox whelps.</p>
<p>Bawds jog, flick quartz, vex nymphs.</p>
</body>
</html>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, "html.parser")
#print (soup.unwrap())
for tag in soup.find_all():
tag.unwrap()
print (soup)