简体   繁体   中英

save Python script output as .html

I have completed my python script which when run in the terminal does what I need it to do. What is the best way to run the script and save its output to a windows folder as a.html file. Also if there's a cleaner way to set this code up I'm open to suggestions?

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'))

Print(soup)

Add this at the end and replace it with your path.

with open("your-path/output.html", "w") as f:
    f.write(soup)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM