1

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)

0

1 Answer 1

3

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

with open("your-path/output.html", "w") as f:
    f.write(soup)
Sign up to request clarification or add additional context in comments.

2 Comments

with open("C:/Folder\Folder\Folder\\Folder/output.html", "w") as f: f.write(soup) give me (unicode error) i tried c:\ but then TypeError write() arguement must be string
If you are using python2, use "wb" mode. For python3, use "w" mode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.