”;
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
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
Advertisements
”;