Neculai Fântânaru

Everything Depends On The Leader

Python: Extracts The Data From The Html Web Page And Automatically Sends Email

On June 24, 2023
, in
Python Scripts Examples by Neculai Fantanaru

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---

Alatura-te Comunitatii Neculai Fantanaru
The 63 Greatest Qualities of a Leader
Cele 63 de calităţi ale liderului

Why read this book? Because it is critical to optimizing your performance. Because it reveals the main coordinates after that are build the character and skills of the leaders, highlighting what it is important for them to increase their influence.

Leadership - Magic of Mastery
Atingerea maestrului

The essential characteristic of this book in comparison with others on the market in the same domain is that it describes through examples the ideal competences of a leader. I never claimed that it's easy to become a good leader, but if people will...

The Master Touch
Leadership - Magia măiestriei

For some leaders, "leading" resembles more to a chess game, a game of cleverness and perspicacity; for others it means a game of chance, a game they think they can win every time risking and betting everything on a single card.

Leadership Puzzle
Leadership Puzzle

I wrote this book that conjoins in a simple way personal development with leadership, just like a puzzle, where you have to match all the given pieces in order to recompose the general image.

Performance in Leading
Leadership - Pe înţelesul tuturor

The aim of this book is to offer you information through concrete examples and to show you how to obtain the capacity to make others see things from the same angle as you.

Leadership for Dummies
Leadership - Pe înţelesul tuturor

Without considering it a concord, the book is representing the try of an ordinary man - the author - who through simple words, facts and usual examples instills to the ordinary man courage and optimism in his own quest to be his own master and who knows... maybe even a leader.