I have to copy a part of one document to another, but I don't want to modify the document I copy from.
If I use .extract() it removes the element from the tree. If I just append selected element like document2.append(document1.tag) it still removes the element from document1.
As I use real files I can just not save document1 after modification, but is there any way to do this without corrupting a document?
It may not be the fastest solution, but it is short and seems to work...
clonedtag = BeautifulSoup(str(sourcetag)).body.contents[0]
BeautifulSoup creates an extra <html><body>...</body></html> around the cloned tag (in order to make the "soup" a sane html document). .body.contents[0] removes those wrapping tags.
This idea was derived Peter Woods comment above and Clemens Klein-Robbenhaar's comment below.
clone element with beautifulsoup