将Python脚本输出另存为.html

2024-05-20 00:01:22 发布

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

我已经完成了我的python脚本,当它在终端中运行时,它完成了我需要它做的事情。 运行脚本并将其输出保存为.html文件到windows文件夹的最佳方式是什么。另外,如果有一个更干净的方法来设置这个代码,我愿意接受建议吗

from bs4 import BeautifulSoup
import re



with open(r'c:/folder/hello.html', 'r') as f:
   html_string = f.read()


soup = BeautifulSoup(html_string)

target = soup.find_all(text=re.compile(r'Note Id'))
for v in target:
    v.replace_with(v.replace('Note Id','Note Id_Test'))

target1 = soup.find_all(text=re.compile(r'Entity Id'))
for v in target1:
    v.replace_with(v.replace('Entity Id','Entity Id_Test'))

target2 = soup.find_all(text=re.compile(r'Entity Name'))
for v in target2:
    v.replace_with(v.replace('Entity Name','Entity Name_Test'))

target3 = soup.find_all(text=re.compile(r'Note Detail'))
for v in target3:
    v.replace_with(v.replace('Note Detail','Note Detail_Test'))

target4 = soup.find_all(text=re.compile(r'Create Date'))
for v in target4:
    v.replace_with(v.replace('Create Date','Create Date_Test'))

印花(汤)


Tags: textintestreidforhtmlwith