如何复制和替换Python中的元素及其子元素?

2024-10-02 22:38:14 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我有两个HTML文件,它们都有一个ID为htmlbody的div。我想检查一个文件中的htmlbody元素是否与另一个文件中的htmlbody元素相同。如果不是,那么我想复制htmlbody元素并将其替换到不同的文件中。请看下面我的代码。你知道吗

我试过在这里使用修改树文档https://www.crummy.com/software/BeautifulSoup/bs4/doc/#append

import codecs
from bs4 import BeautifulSoup 

def getMainFile():
    #opens and pareses the main html file
    main_html = codecs.open("index.html", 'r')
    soup = BeautifulSoup(main_html, 'html.parser')
    #assignes the HTML content of the main file to a variable.
    html_content = soup.find(id="htmlbody")
    return html_content

#User Html file
  def getUserFile():
     user_html = codecs.open("userone.html", 'r')
     soup = BeautifulSoup(user_html, 'html.parser')
     soup.prettify()
     html_content = soup.find(id="htmlbody")
     return html_content


 #Checks files
 if getMainFile() == getUserFile():
    print("all good")
 else:
    new_content = getMainFile()
    user_html = codecs.open("userone.html", 'r')
    soup = BeautifulSoup(user_html, 'html.parser')

  with open("userone.html", "w") as file:
      file.write(str(soup.prettify()))

Tags: 文件theparser元素mainhtmlopencontent