Skip to content

Instantly share code, notes, and snippets.

@me-suzy
Created June 27, 2023 15:29
Show Gist options
  • Save me-suzy/a387b829a3b34f121138e9075e0af20b to your computer and use it in GitHub Desktop.
Save me-suzy/a387b829a3b34f121138e9075e0af20b to your computer and use it in GitHub Desktop.
PERFECTO-2.py
import urllib.request
import re
from bs4 import BeautifulSoup
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# 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.encode('utf-8').decode('utf-8'), flush=True) # Afisează 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)
# Deschide fisierul HTML pentru modificare
with open(r"c:\Folder8\online.html", "r", encoding='utf-8') as file:
online_html = file.read()
# Creați o copie de rezervă a fisierului online.html
with open(r"c:\Folder8\online_backup.html", "w", encoding='utf-8') as backup_file:
backup_file.write(online_html)
# Înlocuieste "TITLU-ARTICOL" cu titlul
online_html = online_html.replace("TITLU-ARTICOL", title)
# Înlocuieste "LINK-CANONICAL" cu canonical
online_html = online_html.replace("LINK-CANONICAL", canonical)
# Înlocuieste "COMENTARIU-BUTON" cu link-ul canonical
online_html = online_html.replace("COMENTARIU-BUTON", canonical)
# Înlocuieste "COMENTARIU-LINK" cu link-ul canonical
online_html = online_html.replace("COMENTARIU-LINK", canonical)
# Înlocuieste "ARTICOL-BEBE" cu article_content
online_html = online_html.replace("ARTICOL-BEBE", article_content)
print("Fisierul HTML a fost citit cu succes!", flush=True)
# 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ă fisierul 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("Fisierul HTML a fost modificat cu succes!", flush=True)
# Trimite email-ul
sender_email = '[email protected]'
sender_password = 'l50_8T+fN?&4@m,(>9}'
receiver_emails = ['[email protected]']
message = MIMEMultipart()
message['From'] = sender_email
message['To'] = ', '.join(receiver_emails)
message['Subject'] = title
message.attach(MIMEText(str(soup_online), 'html'))
with smtplib.SMTP_SSL('mail.neculaifantanaru.com', 465) as smtp_server:
smtp_server.login(sender_email, sender_password)
smtp_server.send_message(message)
print("Email trimis cu succes!", flush=True)
else:
print("Nu s-a găsit conținutul articolului conform cerinței.", flush=True)
else:
print("Nu s-a găsit un link conform cerinței.", flush=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment