-
-
Save me-suzy/2a896f625d6fe8953ed0f23e94f9e7e6 to your computer and use it in GitHub Desktop.
Schimba Categorii
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import re | |
# Calea către folderul cu fișierele HTML | |
folder_path = r'e:\Carte\BB\17 - Site Leadership\Principal 2022\ro' | |
# Calea către fișierul categorii.json | |
json_file_path = os.path.join(folder_path, 'categorii.json') | |
# Deschide fișierul categorii.json și încarcă conținutul | |
with open(json_file_path, 'r') as json_file: | |
data = json.load(json_file) | |
# Creează un dicționar pentru a accesa rapid datele din categorii.json | |
data_dict = {entry['html_file']: entry['line_count'] for entry in data} | |
# Iterează prin fiecare fișier HTML din folder | |
for filename in os.listdir(folder_path): | |
if filename.endswith('.html'): | |
file_path = os.path.join(folder_path, filename) | |
try: | |
# Deschide fișierul HTML și citește conținutul | |
with open(file_path, 'r', encoding='utf-8') as html_file: | |
content = html_file.read() | |
# Expresie regulată actualizată pentru a captura întregul div | |
pattern = r'(<div class="categories-name">\s*<div class="categories-name">\s*<a href="https://neculaifantanaru\.com/(.*?)" title=".*?">\s*<p class="font-16 color-grey text-capitalize"><i class="fa fa-angle-right font-14 color-blue mr-1"></i>.*?<span>)(\d+)(</span>)' | |
def replace_count(match): | |
full_match, href, current_count = match.group(1), match.group(2), match.group(3) | |
if href in data_dict: | |
new_count = data_dict[href] | |
print(f"Replacing in {filename}: {href} - Old count: {current_count}, New count: {new_count}") | |
return f"{full_match}{new_count}{match.group(4)}" | |
return match.group(0) | |
# Aplică înlocuirea | |
new_content, count = re.subn(pattern, replace_count, content) | |
# Verifică dacă s-au făcut modificări | |
if count > 0: | |
# Actualizează conținutul în fișierul HTML | |
with open(file_path, 'w', encoding='utf-8') as updated_html_file: | |
updated_html_file.write(new_content) | |
print(f"Modified file: {filename} - {count} replacements made") | |
else: | |
print(f"No changes in file: {filename}") | |
except Exception as e: | |
print(f"Error processing file: {filename}") | |
print(str(e)) | |
print("Processing complete.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment