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.
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.
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.
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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
