How to delete html file strings in a folder in bulk

Asked 2 years ago, Updated 2 years ago, 51 views

I'm thinking about implementing it with Python
in all html files within a particular folder I want to remove all strings between <script~~~/script>ㅠ<
I tried it with an editor, but there were so many contents in the range between~~ that it didn't work properly...
It's not Python, but other languages are good. Can anyone help me?ㅠ<

python c# c++ visual-studio

2022-09-20 19:53

1 Answers

# Just look at the idea that it can be solved like this with a regular expression.
import re

# Loads all file content. I haven't tested this part, but it'll probably work.
with open('Path/to/file', 'r') as content_file:
    content = content_file.read()

# Step 1. Remove all line changes.
content = re.sub(r"\n", "", content, 0, re.MULTILINE)

# Step 2. Change the line whenever </script> appears.
content = re.sub(r"<\/script>", "</script>\n", content, 0, re.MULTILINE)

# Step 3. You can now fit the pattern <script something> and that </script> for each line.
# Find it and send it all away.
content = re.sub(r"<script.*\/script>\n", "", content, 0, re.MULTILINE)

# Overwrite and you're done
with open('Path/to/file', 'w') as overwrite:
    overwrite.write(content)


2022-09-20 19:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.