21
votes

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?

3
Perhaps consider using the extract method but copy the tag's tree location prior to this. Then add the extracted tag back to its original location (using the location information you've copied) and to its new location. - dilbert
@dilbert: that doesn't work; you'd not have a copy yet so inserting the element into multiple locations just updates the parent and sibling references. A deepcopy of an .extract() / deepcopy / reinsert operation would not work either as NavigableString objects make the assumption that they are immutable and thus implement a __copy__ method that returns self but ignore the fact that they have parent and next_sibling and previous_sibling references that are mutable. - Martijn Pieters♦
@MartijnPieters: A stab in the dark on my part. I thought BS was more modular than that, allowing multiple remove and re-insert operations. - dilbert

3 Answers

29
votes

There is no native clone function in BeautifulSoup in versions before 4.4 (released July 2015); you'd have to create a deep copy yourself, which is tricky as each element maintains links to the rest of the tree.

To clone an element and all its elements, you'd have to copy all attributes and reset their parent-child relationships; this has to happen recursively. This is best done by not copying the relationship attributes and re-seat each recursively-cloned element:

from bs4 import Tag, NavigableString

def clone(el):
    if isinstance(el, NavigableString):
        return type(el)(el)

    copy = Tag(None, el.builder, el.name, el.namespace, el.nsprefix)
    # work around bug where there is no builder set
    # https://bugs.launchpad.net/beautifulsoup/+bug/1307471
    copy.attrs = dict(el.attrs)
    for attr in ('can_be_empty_element', 'hidden'):
        setattr(copy, attr, getattr(el, attr))
    for child in el.contents:
        copy.append(clone(child))
    return copy

This method is kind-of sensitive to the current BeautifulSoup version; I tested this with 4.3, future versions may add attributes that need to be copied too.

You could also monkeypatch this functionality into BeautifulSoup:

from bs4 import Tag, NavigableString


def tag_clone(self):
    copy = type(self)(None, self.builder, self.name, self.namespace, 
                      self.nsprefix)
    # work around bug where there is no builder set
    # https://bugs.launchpad.net/beautifulsoup/+bug/1307471
    copy.attrs = dict(self.attrs)
    for attr in ('can_be_empty_element', 'hidden'):
        setattr(copy, attr, getattr(self, attr))
    for child in self.contents:
        copy.append(child.clone())
    return copy


Tag.clone = tag_clone
NavigableString.clone = lambda self: type(self)(self)

letting you call .clone() on elements directly:

document2.body.append(document1.find('div', id_='someid').clone())

My feature request to the BeautifulSoup project was accepted and tweaked to use the copy.copy() function; now that BeautifulSoup 4.4 is released you can use that version (or newer) and do:

import copy

document2.body.append(copy.copy(document1.find('div', id_='someid')))
7
votes

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.

0
votes

For Python:

You can copy the parent element like:

import copy
p_copy = copy.copy(soup.p)
print p_copy
# <p>I want <b>pizza</b> and more <b>pizza</b>!</p>

Ref: https://www.crummy.com/software/BeautifulSoup/bs4/doc/ Section: Copying Beautiful Soup objects

Regards.