1. You must have a local "online.html" template page with the design related to the future newsletter message you want to send
2. Extract the newest link from the local index.html page
3. Access that link and read the html page via the "view source" method, directly from the internet page
4. Takes the necessary data from that html page and then copies it to the local "online.html" page. Then backup to an "online_backup.html" file
TITLU-ARTICOL in "online.html" will be completed by the <title> content of the web page
LINK-CANONICAL in "online.html" will be filled by the <canonical> content of the web page
COMENTARIU-BUTON in "online.html" will be filled by the <canonical> content of the web page
COMENTARIU-LINK in "online.html" will be filled by the <canonical> content of the web page
ARTICOL-BEBE in "online.html" will be completed by the content of the web page article. The content is between the landmarks < ! -- ARTICOL START --> and < ! -- ARTICOL FINAL -->
5. Send email to the specified email addresses
6. Do UNDO to the "online.html" file and save.
You can view the full code here: https://pastebin.com/ANidubsK
import urllib.request import re from bs4 import BeautifulSoup import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import shutil # Accesează index.html url = r"file:///e:/Carte/BB/17%20-%20Site%20Leadership/Principal%202022/ro/index.html" # Descarcă conținutul paginii response = urllib.request.urlopen(url) html_content = response.read() # Selectează link-ul conform pattern-ului specificat pattern = r'<h3 class="font-weight-normal" itemprop="name"><a href="([\s\S]*?)" class="color-black">' match = re.search(pattern, html_content.decode()) if match: link_url = match.group(1) print("Link-ul deschis este:", link_url) # Afișează link-ul deschis # Accesează link-ul response = urllib.request.urlopen(link_url) link_content = response.read() # Extrage cerințele soup = BeautifulSoup(link_content, 'html.parser') title = soup.title.string canonical = soup.find('link', {'rel': 'canonical'})['href'] # Extrage conținutul articolului article_pattern = r'<!-- ARTICOL START -->([\s\S]*?)<!-- ARTICOL FINAL -->' article_match = re.search(article_pattern, link_content.decode()) if article_match: article_content = article_match.group(1) # Facem backup la fișierul online.html shutil.copy2(r"c:\Folder8\online.html", r"c:\Folder8\online_backup.html") # Deschide fișierul HTML pentru modificare with open(r"c:\Folder8\online.html", "r", encoding='utf-8') as file: online_html = file.read() # Înlocuiește "TITLU-ARTICOL" cu titlul online_html = online_html.replace("TITLU-ARTICOL", title) # Înlocuiește "LINK-CANONICAL" cu canonical online_html = online_html.replace("LINK-CANONICAL", canonical) # Înlocuiește "COMENTARIU-BUTON" cu link-ul canonical online_html = online_html.replace("COMENTARIU-BUTON", canonical) # Înlocuiește "COMENTARIU-LINK" cu link-ul canonical online_html = online_html.replace("COMENTARIU-LINK", canonical) # Înlocuiește "ARTICOL-BEBE" cu article_content online_html = online_html.replace("ARTICOL-BEBE", article_content) print("Fișierul HTML a fost citit cu succes!") # Formatează tagurile specifice cu BOLD soup_online = BeautifulSoup(online_html, 'html.parser') for tag in soup_online.find_all(['p', 'span'], class_=['text_obisnuit', 'text_obisnuit2']): if tag.get('class') == ['text_obisnuit']: tag.wrap(soup_online.new_tag("span", style="font-weight: normal;")) else: tag.wrap(soup_online.new_tag("strong")) # Actualizează fișierul online.html cu conținutul formatat with open(r"c:\Folder8\online.html", "w", encoding='utf-8') as file: file.write(str(soup_online)) print("Fișierul HTML a fost modificat cu succes!") # Trimite email-ul (configurat pentru Cpanel ) sender_email = 'your@email.com' sender_password = 'PASWWORD' receiver_emails = ['exemple_1@yahoo.com', 'example_2@gmail.com'] message = MIMEMultipart() message['From'] = sender_email message['To'] = ', '.join(receiver_emails) message['Subject'] = 'Articol' message.attach(MIMEText(str(soup_online), 'html')) with smtplib.SMTP_SSL('mail.YOUR-DOMAIN.com', 465) as smtp_server: smtp_server.login(sender_email, sender_password) smtp_server.send_message(message) print("Email trimis cu succes!") # Restaurăm fișierul online.html din backup shutil.copy2(r"c:\Folder8\online_backup.html", r"c:\Folder8\online.html") print("Fișierul online.html a fost restaurat din backup!") else: print("Nu s-a găsit conținutul articolului conform cerinței.") else: print("Nu s-a găsit un link conform cerinței.")
That's all folks.
Also, see my other Python Scripts ---HERE---