Advertisement
nicuf

Beaut

Jun 27th, 2023 (edited)
1,283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. import re
  3.  
  4. # Deschide și citește conținutul fișierelor HTML
  5. with open('c:/Folder7/new-file.html', 'r', encoding='utf-8') as file:
  6.     new_file_content = file.read()
  7.  
  8. with open('c:/Folder7/old-file.html', 'r', encoding='utf-8') as file:
  9.     old_file_content = file.read()
  10.  
  11. # Creează obiecte BeautifulSoup
  12. old_soup = BeautifulSoup(old_file_content, 'html.parser')
  13. new_soup = BeautifulSoup(new_file_content, 'html.parser')
  14.  
  15. # Extrage datele necesare din new_file.html
  16. item_id_match = re.search('<!-- \$item_id = (\d+) ;', new_file_content)
  17. item_id = item_id_match.group(1) if item_id_match else None
  18.  
  19. title = new_soup.title.string if new_soup.title else None
  20.  
  21. canonical_link_element = new_soup.find("link", {"rel": "canonical"})
  22. canonical_link = canonical_link_element['href'] if canonical_link_element else None
  23.  
  24. meta_description_element = new_soup.find("meta", {"name": "description"})
  25. meta_description = meta_description_element['content'] if meta_description_element else None
  26.  
  27. article_match = re.search('<!-- ARTICOL START -->(.*?)<!-- ARTICOL FINAL -->', new_file_content, re.DOTALL)
  28. article = article_match.group(1) if article_match else None
  29.  
  30. # Înlocuiește datele în old_file.html
  31. if item_id:
  32.     old_file_content = re.sub('<!-- \$item_id = \d+ ;', f'<!-- $item_id = {item_id} ;', old_file_content)
  33.  
  34. if title and old_soup.title:
  35.     old_soup.title.string.replace_with(title)
  36.  
  37. if canonical_link:
  38.     canonical_link_element_old = old_soup.find("link", {"rel": "canonical"})
  39.     if canonical_link_element_old:
  40.         canonical_link_element_old['href'] = canonical_link
  41.  
  42. if meta_description:
  43.     meta_description_element_old = old_soup.find("meta", {"name": "description"})
  44.     if meta_description_element_old:
  45.         meta_description_element_old['content'] = meta_description
  46.  
  47. if article:
  48.     old_file_content = re.sub('<!-- ARTICOL START -->(.*?)<!-- ARTICOL FINAL -->', f'<!-- ARTICOL START -->{article}<!-- ARTICOL FINAL -->', old_file_content, flags=re.DOTALL)
  49.  
  50. # Salvează modificările în old_file.html
  51. with open('c:/Folder7/old-file.html', 'w', encoding='utf-8') as
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement