gpt4 book ai didi

python - 在原始 HTML 文件上写回 Beautifulsoup 时出错

转载 作者:可可西里 更新时间:2023-11-01 11:18:03 25 4
gpt4 key购买 nike

我的原始 HTML 文件的 BeautifulSoup 副本的编码可能有问题吗?

我被告知我无法写入文件,因为我必须写入一个 str 而不是 none。

请看下面的代码和TypeError:

#Manipulating HTML and saving changed with BeautifulSoup

#Importing libraries
from bs4 import BeautifulSoup

#Opening the local HTML file
site_html = open(r"C:\Users\rbaden\desktop\KPI_Site\index.html")

#Creating Soup from source HTML file
soup =BeautifulSoup(site_html)
#print(soup.prettify())

#Locate and view specified class in HTML file
test = soup.find_all(class_='test-message-one')
print(test)

#Test place holder for a python variable that should replace the specified class
var = ('Testing...456')

#Replace the class in soup redition of HTML
for i in soup.find_all(class_='test-message-one'):
i.string = var

#overwriting the source HTML file on local drive
with open(r"C:\Users\rbaden\desktop\KPI_Site\index.html") as f:
f.write(soup.content)

enter image description here

最佳答案

首先,您需要以w 模式打开文件。

并且,您需要编写 str(soup)soup.prettify():

with open(r"C:\Users\rbaden\desktop\KPI_Site\index.html", "w") as f:
f.write(soup.prettify())

关于python - 在原始 HTML 文件上写回 Beautifulsoup 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29263372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com