Skip to content

Instantly share code, notes, and snippets.

@maaduukaar
Created June 30, 2025 11:56
Show Gist options
  • Save maaduukaar/7cca3d3cd0f2060e52ea5d3ce5776895 to your computer and use it in GitHub Desktop.
Save maaduukaar/7cca3d3cd0f2060e52ea5d3ce5776895 to your computer and use it in GitHub Desktop.
import os
def replace_in_html_files(pattern, replacement, folder_path):
# Счетчики для статистики
total_files = 0
processed_files = 0
files_with_matches = 0
files_without_matches = 0
total_matches = 0
print("Начинаю обработку HTML файлов...")
print("-" * 50)
for filename in os.listdir(folder_path):
if filename.endswith('.html'):
total_files += 1
file_path = os.path.join(folder_path, filename)
print(f"Обрабатываю файл: {filename}")
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# Подсчитываем количество вхождений
match_count = content.count(pattern)
if match_count > 0:
# Заменяем все вхождения
new_content = content.replace(pattern, replacement)
with open(file_path, 'w', encoding='utf-8') as file:
file.write(new_content)
files_with_matches += 1
total_matches += match_count
print(f" ✓ Найдено и заменено совпадений: {match_count}")
else:
files_without_matches += 1
print(f" - Паттерн не найден")
processed_files += 1
except Exception as e:
print(f" ✗ Ошибка при обработке: {e}")
# Статистика
print("-" * 50)
print("СТАТИСТИКА ОБРАБОТКИ:")
print(f"Всего HTML файлов: {total_files}")
print(f"Обработано файлов: {processed_files}")
print(f"Файлов с совпадениями: {files_with_matches}")
print(f"Общее количество замен: {total_matches}")
# Ваши многострочные переменные
pattern = """
<!-- nominify begin --> <script>
(function(w,d,u){
var s=d.createElement('script');s.async=true;s.src=u+'?'+(Date.now()/60000|0);
var h=d.getElementsByTagName('script')[0];h.parentNode.insertBefore(s,h);
})(window,document,'https://cdn-ru.bitrix24.ru/b21504426/crm/site_button/loader_3_7auifb.js');
</script> <!-- nominify end -->
"""
replacement = """
<!-- nominify begin закомментирован bitrix <script>
(function(w,d,u){
var s=d.createElement('script');s.async=true;s.src=u+'?'+(Date.now()/60000|0);
var h=d.getElementsByTagName('script')[0];h.parentNode.insertBefore(s,h);
})(window,document,'https://cdn-ru.bitrix24.ru/b21504426/crm/site_button/loader_3_7auifb.js');
</script> nominify end -->
"""
# Запуск
if pattern.strip() and replacement.strip():
replace_in_html_files(pattern, replacement, '.')
else:
print("Заполните переменные pattern и replacement!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment