Advertisement
nicuf

send newsletter

Jun 24th, 2023
934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.23 KB | None | 0 0
  1. -----EXPLANATION--------
  2. ROMANA: https://neculaifantanaru.com/python-extrage-datele-din-pagina-web-html-si-trimite-automat-email.html
  3. ENGLEZA: https://neculaifantanaru.com/en/extracts-the-data-from-the-html-web-page-and-automatically-sends-email.html
  4. ------------------------
  5.  
  6.  
  7. import urllib.request
  8. import re
  9. from bs4 import BeautifulSoup
  10. import smtplib
  11. from email.mime.text import MIMEText
  12. from email.mime.multipart import MIMEMultipart
  13. import shutil
  14.  
  15. # Accesează index.html
  16. url = r"file:///e:/Carte/BB/17%20-%20Site%20Leadership/Principal%202022/ro/index.html"
  17.  
  18. # Descarcă conținutul paginii
  19. response = urllib.request.urlopen(url)
  20. html_content = response.read()
  21.  
  22. # Selectează link-ul conform pattern-ului specificat
  23. pattern = r'<h3 class="font-weight-normal" itemprop="name"><a href="([\s\S]*?)" class="color-black">'
  24. match = re.search(pattern, html_content.decode())
  25. if match:
  26.     link_url = match.group(1)
  27.     print("Link-ul deschis este:", link_url)  # Afișează link-ul deschis
  28.  
  29.     # Accesează link-ul
  30.     response = urllib.request.urlopen(link_url)
  31.     link_content = response.read()
  32.  
  33.     # Extrage cerințele
  34.     soup = BeautifulSoup(link_content, 'html.parser')
  35.     title = soup.title.string
  36.     canonical = soup.find('link', {'rel': 'canonical'})['href']
  37.  
  38.     # Extrage conținutul articolului
  39.     article_pattern = r'<!-- ARTICOL START -->([\s\S]*?)<!-- ARTICOL FINAL -->'
  40.     article_match = re.search(article_pattern, link_content.decode())
  41.     if article_match:
  42.         article_content = article_match.group(1)
  43.  
  44.         # Facem backup la fișierul online.html
  45.         shutil.copy2(r"c:\Folder8\online.html", r"c:\Folder8\online_backup.html")
  46.  
  47.         # Deschide fișierul HTML pentru modificare
  48.         with open(r"c:\Folder8\online.html", "r", encoding='utf-8') as file:
  49.             online_html = file.read()
  50.  
  51.         # Înlocuiește "TITLU-ARTICOL" cu titlul
  52.         online_html = online_html.replace("TITLU-ARTICOL", title)
  53.  
  54.         # Înlocuiește "LINK-CANONICAL" cu canonical
  55.         online_html = online_html.replace("LINK-CANONICAL", canonical)
  56.  
  57.         # Înlocuiește "COMENTARIU-BUTON" cu link-ul canonical
  58.         online_html = online_html.replace("COMENTARIU-BUTON", canonical)
  59.  
  60.         # Înlocuiește "COMENTARIU-LINK" cu link-ul canonical
  61.         online_html = online_html.replace("COMENTARIU-LINK", canonical)
  62.  
  63.         # Înlocuiește "ARTICOL-BEBE" cu article_content
  64.         online_html = online_html.replace("ARTICOL-BEBE", article_content)
  65.  
  66.         print("Fișierul HTML a fost citit cu succes!")
  67.  
  68.         # Formatează tagurile specifice cu BOLD
  69.         soup_online = BeautifulSoup(online_html, 'html.parser')
  70.         for tag in soup_online.find_all(['p', 'span'], class_=['text_obisnuit', 'text_obisnuit2']):
  71.             if tag.get('class') == ['text_obisnuit']:
  72.                 tag.wrap(soup_online.new_tag("span", style="font-weight: normal;"))
  73.             else:
  74.                 tag.wrap(soup_online.new_tag("strong"))
  75.  
  76.         # Actualizează fișierul online.html cu conținutul formatat
  77.         with open(r"c:\Folder8\online.html", "w", encoding='utf-8') as file:
  78.             file.write(str(soup_online))
  79.  
  80.         print("Fișierul HTML a fost modificat cu succes!")
  81.  
  82.         # Trimite email-ul (configurat pentru Cpanel )
  83.         sender_email = 'your@email.com'
  84.         sender_password = 'PASWWORD'
  85.         receiver_emails = ['exemple_1@yahoo.com', 'example_2@gmail.com']
  86.  
  87.         message = MIMEMultipart()
  88.         message['From'] = sender_email
  89.         message['To'] = ', '.join(receiver_emails)
  90.         message['Subject'] = 'Articol'
  91.  
  92.         message.attach(MIMEText(str(soup_online), 'html'))
  93.  
  94.         with smtplib.SMTP_SSL('mail.YOUR-DOMAIN.com', 465) as smtp_server:
  95.             smtp_server.login(sender_email, sender_password)
  96.             smtp_server.send_message(message)
  97.  
  98.         print("Email trimis cu succes!")
  99.  
  100.         # Restaurăm fișierul online.html din backup
  101.         shutil.copy2(r"c:\Folder8\online_backup.html", r"c:\Folder8\online.html")
  102.  
  103.         print("Fișierul online.html a fost restaurat din backup!")
  104.     else:
  105.         print("Nu s-a găsit conținutul articolului conform cerinței.")
  106. else:
  107.     print("Nu s-a găsit un link conform cerinței.")
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement