如何在Python中用Beautiful Soup复制和替换元素及其子元素?
我有两个HTML文件,它们都有一个ID为htmlbody的div。我想检查一个文件中的htmlbody元素是否与另一个文件中的htmlbody元素相同。如果不是,那么我想复制htmlbody元素并将其替换到不同的文件中。请看下面我的代码 我尝试过在这里修改树文档如何在Python中用Beautiful Soup复制和替换元素及其子元素?,python,html,beautifulsoup,Python,Html,Beautifulsoup,我有两个HTML文件,它们都有一个ID为htmlbody的div。我想检查一个文件中的htmlbody元素是否与另一个文件中的htmlbody元素相同。如果不是,那么我想复制htmlbody元素并将其替换到不同的文件中。请看下面我的代码 我尝试过在这里修改树文档 您得到的错误/异常有哪些?markup=markup.read()TypeError:'NoneType'对象不可调用,并且AttributeError:'NoneType'对象没有属性'replace_' import codecs
您得到的错误/异常有哪些?markup=markup.read()TypeError:'NoneType'对象不可调用,并且AttributeError:'NoneType'对象没有属性'replace_'
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()))