Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----EXPLANATION--------
- ROMANA: https://neculaifantanaru.com/python-extrage-datele-din-pagina-web-html-si-trimite-automat-email.html
- ENGLEZA: https://neculaifantanaru.com/en/extracts-the-data-from-the-html-web-page-and-automatically-sends-email.html
- ------------------------
- 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.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement