遞迴掃描指定目錄下所有 .html/.htm 檔案,將 <script>...</script> 標籤**停用** (改寫為無害屬性) 而非刪除,保留原始碼供審閱。適用於離線瀏覽下載的網站 (如 HTTrack) 去除追蹤碼、廣告、惡意腳本。
strip_scripts.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
strip_scripts.py - 批量停用 HTML 中的 Script 標籤
Usage: python strip_scripts.py
Example: python strip_scripts.py /path/to/downloaded_site
"""
import os
import sys
import re
SCRIPT_PATTERN = re.compile(r'', re.IGNORECASE | re.DOTALL)
REPLACEMENT = '