Beautiful Soup 简明教程

Beautiful Soup - unwrap() Method

Method Description

unwrap() 方法与 wrap() 方法相反。它用标签内部的任何内容替换该标签。它从元素中移除标签并返回它。

Syntax

unwrap()

Parameters

此方法不需要任何参数。

Return Type

unwrap() 方法返回已移除的标签。

Example 1

在以下示例中,从 HTML 字符串中移除 <b> 标签。

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)

Output

<p>The quick, brown fox jumps over a lazy dog.</p>

Example 2

下面的代码打印 unwrap() 方法的返回的值。

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)

Output

<b></b>

Example 3

unwrap() 方法非常适合剥离标记,如下代码所示:

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)

Output

The quick, brown fox jumps over a lazy dog.
DJs flock by when MTV ax quiz prog.
Junk MTV quiz graced by fox whelps.
Bawds jog, flick quartz, vex nymphs.