Beautiful Soup - Functions Reference

Beautiful Soup Useful Resources

Beautiful Soup - Copying Objects



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

Example - Copying a tag

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>

Example - Comparing copied tags

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

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 (i1 == icopy)
print (i1 is icopy)

Output

True
False

Example - Comparing copied tag parent

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

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.parent)

Output

None
Advertisements