Automate Daily Weather Emails with Python Web Scraping

This tutorial shows how to scrape real-time weather data from Sohu Weather using BeautifulSoup and automatically send it via email with Python's smtplib, including code walkthroughs for both Sohu and 163 email servers, a reference list of common SMTP servers, and PM2.5 air quality standards.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Automate Daily Weather Emails with Python Web Scraping

Overview

This article demonstrates how to build a Python script that automatically collects weather data from Sohu Weather and sends it to specified email addresses on a daily schedule.

Scraping Weather Data with BeautifulSoup

The target website is Sohu Weather (sohu.com). The script uses the requests library to fetch the page and BeautifulSoup to parse HTML. The get_weather function extracts: current city, weather condition, current temperature, minimum temperature, maximum temperature, air quality index, and PM2.5 value. The author recommends testing selectors incrementally to avoid errors.

Sohu Weather page showing target data fields
Sohu Weather page showing target data fields
get_weather function code using BeautifulSoup selectors
get_weather function code using BeautifulSoup selectors

Sending Email via SMTP

The send_email function uses Python's smtplib and email.mime.text.MIMEText to compose and send an HTML email. The sender's email address, password (or app-specific password), and SMTP server are required. Recipient emails are stored in a list and iterated over.

send_email function code using smtplib
send_email function code using smtplib

Testing with Sohu and 163 Email Servers

The author tests with a Sohu email account (SMTP server: smtp.sohu.com) targeting Dalian city. The script runs successfully and delivers an email with the scraped weather data. The same script works with a 163 email account (SMTP server: smtp.163.com) by only changing the server address.

Script output showing successful email send
Script output showing successful email send
Received email in Sohu inbox with weather data
Received email in Sohu inbox with weather data
Sending to 163 email server
Sending to 163 email server
Received email in 163 inbox
Received email in 163 inbox

Common SMTP Server Reference

QQ Mail: smtp.qq.com

126 Mail: smtp.126.com

163 Mail: smtp.163.com

Sina Mail: smtp.sina.com.cn

Sohu Mail: smtp.sohu.com

Yahoo Mail: smtp.mail.yahoo.com

Gmail: smtp.gmail.com

PM2.5 Air Quality Reference

The article includes a reference table for PM2.5 24-hour average concentrations:

Excellent: ≤35 µg/m³

Good: 35–75 µg/m³

Light Pollution: 75–115 µg/m³

Moderate Pollution: 115–150 µg/m³

Heavy Pollution: 150–250 µg/m³

Severe Pollution: ≥250 µg/m³

The scraped data for Dalian shows PM2.5 at 110 µg/m³, falling in the Light Pollution range.

Key Takeaways

By combining web scraping with SMTP email automation, developers can build scheduled notification systems for any periodically updated web data. The article emphasizes incremental testing of selectors and provides a ready-to-adapt template for both Sohu and 163 email services.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythonweb scrapingBeautifulSoupSMTPscheduled tasksemail automationweather dataPM2.5
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.