Automate Book List Emails with Python: Web Scraping & Scheduled Sending
This tutorial shows how to build a Python web‑scraper that extracts book titles and authors from a website, formats the data, and automatically sends it via email on a scheduled interval using SMTP and the schedule library.
Introduction
The author shares a small project that combines a Python web‑scraper with automated email delivery. The goal is to fetch a book list from a website, format the information, and send it to a specified mailbox at regular intervals.
Idea
The approach consists of three steps: create a spider to crawl the target site, generate the email content, and set up a timed task that triggers the sending process.
Implementation
The complete code is provided below. Replace the three placeholders marked in red in the original screenshots: the sender's QQ email address, the QQ email authorization code, and the recipient's email address.
# -*- coding: utf-8 -*-
import requests, bs4
import smtplib
import schedule
import time
from bs4 import BeautifulSoup
from email.mime.text import MIMEText
from email.header import Header
# Configuration (replace with your own credentials)
account = '{0}'.format('[email protected]')
password = '{0}'.format('awmowqginzdijg')
receiver = '{0}'.format('[email protected]')
# Spider: fetch book titles and authors from sobooks.cc
def recipe_spider():
list_all = ''
num = 0
for a in range(1, 3):
url = f'https://sobooks.cc/page/{a}'
res = requests.get(url)
res.encoding = res.apparent_encoding
bs = BeautifulSoup(res.text, 'html.parser')
books = bs.find_all('h3')
authors = bs.find_all('p')
for i in range(len(books)):
num += 1
book = books[i].text.strip()
author = authors[i+1].text.strip()
entry = f'书名{num}: {book},作者: {author}
'
list_all += entry
return list_all
# Send email with the fetched content
def send_email(list_all):
mailhost = 'smtp.qq.com'
qqmail = smtplib.SMTP()
qqmail.connect(mailhost, 25)
qqmail.login(account, password)
content = '亲爱的,今天书单' + list_all
message = MIMEText(content, 'plain', 'utf-8')
subject = '今天看什么'
message['Subject'] = Header(subject, 'utf-8')
try:
qqmail.sendmail(account, receiver, message.as_string())
print('邮件发送成功')
except Exception:
print('邮件发送失败')
qqmail.quit()
# Job executed by the scheduler
def job():
print('开始一次任务')
list_all = recipe_spider()
send_email(list_all)
print('任务完成')
if __name__ == '__main__':
# Run the job every 0.05 minutes (3 seconds) for demonstration
schedule.every(0.05).minutes.do(job)
while True:
schedule.run_pending()
time.sleep(1)Common Issues
Typical problems include incorrect email address formatting or an invalid authorization code, which cause the SMTP login to fail. Verify that the email suffix is correct and that the authorization code is copied completely.
To obtain a QQ email authorization code, open QQ Mail, go to Settings → Account, scroll to the bottom, click “Generate Authorization Code”, and copy the displayed string into the script.
Conclusion
The article demonstrates how to combine web scraping, email composition, and scheduled execution in Python to create an automated book‑list notification system. Readers are encouraged to try the code, adjust the credentials, and explore further enhancements.
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.
