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.

Syntax

unwrap()

Parameters

此方法不需要任何参数。

The method doesn’t require any parameter.

Return Type

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

The unwrap() method returns the tag that has been removed.

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)

Output

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

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)

Output

<b></b>

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)

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.