Beautiful Soup 简明教程

Beautiful Soup - Copying Objects

要创建任何标记或 NavigableString 的副本,请使用 Python 标准库中 copy 模块中的 copy() 函数。

To create a copy of any tag or NavigableString, use copy() function from the copy module from Python’s standard library.

Example

from bs4 import BeautifulSoup
import copy

markup = "<p>Learn <b>Python, Java</b>, <i>advanced Python and advanced Java</i>! from Tutorialspoint</p>"
soup = BeautifulSoup(markup, "html.parser")
i1 = soup.find('i')
icopy = copy.copy(i1)

print (icopy)

Output

<i>advanced Python and advanced Java</i>

虽然两个副本(原副本和已复制副本)包含相同的标记,但这两个副本并不表示相同的对象。

Although the two copies (original and copied one) contain the same markup however, the two do not represent the same object.

print (i1 == icopy)
print (i1 is icopy)

Output

True
False

已复制对象与原始 Beautiful Soup 对象树完全分离,就像对其调用了 extract() 一样。

The copied object is completely detached from the original Beautiful Soup object tree, just as if extract() had been called on it.

print (icopy.parent)

Output

None