Puteţi vizualiza întregul cod aici: https://pastebin.com/awhXMcG3
Următoarele taguri trebuie să se afle atât în fişierul html din Folderul A, cât şi în celelalte fişiere html din Folderul B. Codul Python va face parsing la următoarele taguri:
<title></title>, <meta name="description" content=" "/>, all from <!-- ARTICLE START --> to <!-- ARTICLE FINAL -->, all from <!-- FLAGS_1 --> to <!-- FLAGS -->, all from <!-- MENU START --> to <!-- MENU FINAL -->
Aceasta este structura fişierelor. Trebuie ca atât fişierul din Folder A să aibă aceleaşi taguri html, respectiv aceleaşi secţiuni comentate. Din fişierul html din Folder A se vor copia toate aceste secţiuni în fişierele din Folder B.
Important: Conţinutul tagurilor şi conţinutul comentariilor (Text Text) sunt diferite în fişierul în Folder A fată de fişierele html din Folder B. Aceasta este şi ideea. Vreau ca conţinutul acestor taguri din Folder A să înlocuiască conţinutul aceloraşi taguri din fişierele din Folder B.
De exemplu. Din fişierul example.html (din Folder A) se vor copia următoarele secţiuni în fişierele one.html şi two.html (din Folder B)
<title>YOUR FIRST PAGE</title> <meta name="description" content="I LOVE HTML and CSS"/> <!-- ARTICLE START --> Text Text <!-- ARTICLE FINAL --> <!-- FLAGS_1 --> Text Text <!-- FLAGS --> <!-- MENU START --> Text Text <!-- MENU FINAL -->
Codul Python:
import requests import re # The folder that contains the file you want to parse english_folder1 = r"d:\Downloads\A" # The folder with the files you want to change english_folder2 = r"d:\Downloads\B" # The file you want to make parsing file_to_parse_from = 'example.html' extension_file = ".html" use_parse_folder = True import os en1_directory = os.fsencode(english_folder1) en2_directory = os.fsencode(english_folder2) print('Going through english folder') for file in os.listdir(en2_directory): filename = os.fsdecode(file) print(filename) if filename == 'y_key_e479323ce281e459.html' or filename == 'directory.html': continue if filename.endswith(extension_file): with open(os.path.join(english_folder1, file_to_parse_from), encoding='utf-8') as html: html = html.read() try: with open(os.path.join(english_folder2, filename), encoding='utf-8') as en_html: en_html = en_html.read() title = re.search('<title.+/title>', html)[0] meta = re.search('<meta name="description".+>', html)[0] comment_body = re.search('<!-- ARTICLE START -->.+<!-- ARTICLE FINAL -->', html, flags=re.DOTALL)[0] try: comment_body2 = re.search('<!-- FLAGS_1 -->.+<!-- FLAGS -->', html, flags=re.DOTALL)[0] en_html = re.sub('<!-- FLAGS_1 -->.+<!-- FLAGS -->', comment_body2, en_html, flags=re.DOTALL) except: pass try: comment_body3 = re.search('<!-- MENU START -->.+<!-- MENU FINAL -->', html, flags=re.DOTALL)[0] en_html = re.sub('<!-- MENU START -->.+<!-- MENU FINAL -->', comment_body3, en_html, flags=re.DOTALL) except: pass en_html = re.sub('<!-- ARTICLE START -->.+<!-- ARTICLE FINAL -->', comment_body, en_html, flags=re.DOTALL) en_html = re.sub('<meta name="description".+>', meta, en_html) en_html = re.sub('<title.+/title>', title, en_html) except FileNotFoundError: continue print(f'{filename} parsed') if use_parse_folder: try: with open(os.path.join(english_folder2+r'\parsed', 'parsed_'+filename), 'w', encoding='utf-8') as new_html: new_html.write(en_html) except: os.mkdir(english_folder2+r'\parsed') with open(os.path.join(english_folder2+r'\parsed', 'parsed_'+filename), 'w', encoding='utf-8') as new_html: new_html.write(en_html) else: with open(os.path.join(english_folder2, 'parsed_'+filename), 'w', encoding='utf-8') as html: html.write(en_html)
That's all folks.
If you like my code, then make me a favor: translate your website into Romanian, "ro".
Also, you can see other Python Codes: VERSION 2 of this code. Or Version 3 OR Version 4 OR Version 5
Puteţi vizualiza şi versiunea de cod în PowerShell or VERSION 2 or VERSION 3