clone element with beautifulsoup

前端 未结 3 496
粉色の甜心
粉色の甜心 2020-12-18 18:48

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

3条回答
  •  轮回少年
    2020-12-18 19:14

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

提交回复
热议问题