Search code examples
pythonhtmlbeautifulsoup

Move a html tag from one html file to another with BeautifulSoup


I have 2 HTML files called A.html and B.html. Each file has exactly 1 tag. I want to replace the "pre" tag in B.html with the one in A.html with BeautifulSoup but I haven't found any solution so far since the A's has some children. How do I do that? Thanks!

A.html

<html>

<head>
  <style>
    <!-- Some CSS -->
  </style>

  <body>
    <pre>
 <b>Db</b>  <b>Eb</b>  <b>Ab</b>  <b>Gb</b>  <b>F</b>  <b>Bbm</b>  <b>Ebm</b> 
</pre>
  </body>

</html>

B.html

<html>

<head>
  <style>
    <!-- Some CSS -->
  </style>

  <body>
    <pre>
 
</pre>
  </body>

</html>


Solution

  • from bs4 import BeautifulSoup
    

    First let's save the files in variables:

    htmlA = open("A.html", "r", encoding='utf-8').read()
    htmlB = open("B.html", "r", encoding='utf-8').read()
    

    Now we will create our soups:

    soupA = BeautifulSoup(htmlA, 'html.parser')
    soupB = BeautifulSoup(htmlB, 'html.parser')
    

    Then we can replace the tags:

    soupB.pre.replace_with(soupA.pre)