Sending Weather Information via Email After Web Crawling
Are you spending unnecessary time searching and organizing large amounts of information?
By building a system that automatically collects and processes the necessary information from the internet and sends notification emails, you can greatly enhance your productivity.
No need to establish a separate data collection or notification system.
In this lesson, we will explore how to fetch weather information
from the weather bureau's website and send the collected data via email
.
Periodically Sending Crawled Weather Information via Email
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from selenium import webdriver
from selenium.webdriver.common.by import By
# Only emails registered with CodeFriends can receive emails
receiver_email = "youremail@example.com"
# Fetch weather information
def get_weather():
# Launch Chrome driver
driver = webdriver.Chrome()
# Navigate to the weather bureau's weather information page
url = "https://www.weather.gov"
driver.get(url)
# Get temperature and feels-like temperature elements
temperature_element = driver.find_element(By.CLASS_NAME, 'temp')
feels_like_element = driver.find_element(By.CLASS_NAME, 'feels-like')
# Store temperature and feels-like temperature information
temperature = temperature_element.text
feels_like = feels_like_element.text
driver.quit()
# Return temperature and feels-like temperature as a tuple
weather_info = (temperature, feels_like)
return weather_info
# Send weather information via email
def send_email(weather_info):
sender_email = "admin_email"
sender_password = "admin_password"
smtp_server = "smtp.gmail.com"
port = 587
# Create the email
msg = MIMEMultipart('alternative')
msg['Subject'] = "Today's Weather Information"
msg['From'] = sender_email
msg['To'] = receiver_email
body = f"""
<html>
<body>
<h1>Today's Weather Information</h1>
<p>Temperature: {weather_info[0]}</p>
<p>Feels Like: {weather_info[1]}</p>
</body>
</html>
"""
# Attach the MIME part
html_email = MIMEText(body, 'html')
msg.attach(html_email)
# Connect to the SMTP server
server = smtplib.SMTP(smtp_server, port)
# Initiate TLS (email encryption)
server.starttls()
# Login to the email account
server.login(sender_email, sender_password)
# Convert the message to a string
text = msg.as_string()
try:
# Send the email
server.sendmail(sender_email, receiver_email, text)
print("Email sent successfully!")
except Exception as e:
print(f"Error occurred while sending email: {e}")
finally:
server.quit()
# Get weather information
weather_info = get_weather()
# Send the email
send_email(weather_info)
The above code uses Selenium to crawl data from the web and send the collected data via email.
If you use the schedule
library to execute this process periodically, you can receive weather information emails at a specific time every day.
Example Code to Send Weather Information Email Every Hour
import schedule
# Use schedule library to send weather information email every hour
schedule.every().hour.do(send_email, get_weather())
# For security reasons, periodic email sending does not work in the practice environment
while True:
schedule.run_pending()
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.