Build a Daily Article Email Bot with Python Web Scraping
This tutorial shows how to use Python to scrape a daily article from a website, format the content, and automatically send it via email using SMTP, with full source code and guidance for scheduling the script.
Preface
In a Python community a member shared an interesting script that fetches a daily article and emails it, which the author decided to reproduce and explain.
Implementation Idea
The solution consists of two parts: a Python web‑scraper that extracts the article from a web page and stores it in a variable, and an email‑sending routine that builds a MIME message and delivers it through an SMTP server.
Implementation Process
Below is the complete source code.
import requests
from lxml import etree
import time
import smtplib
from email.mime.text import MIMEText
from email.header import Header
account = '{0}'.format('请输入你的邮箱:')
password = '{0}'.format('请输入你的密码:')
receiver = '{0}'.format('请输入收件人的邮箱:')
def getArticle(url):
global data
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36'
}
r = requests.get(url, headers=headers)
html = etree.HTML(r.text)
article = html.xpath('//h1/text() | //p[@class="article_author"]/span/text() | //div[@class="article_text"]/p/text()')
data = "%s %s %s %s %s %s %s %s" % (
str(time.ctime()),
'
《', str(article[0]), '
作者:', str(article[1]), '
', '
'.join(article[2:])
)
with open('article.txt', 'w', encoding='utf-8') as f:
f.write(time.ctime() + '
《' + article[0] + '
作者:' + article[1] + '
')
f.write('
'.join(article[2:]))
f.write('
')
print(data)
return data
def send_email(data):
global account, password, receiver
mailhost = 'smtp.qq.com'
qqmail = smtplib.SMTP_SSL(mailhost, 465)
qqmail.login(account, password)
content = '亲爱的,每日一文:' + data
message = MIMEText(content, 'plain', 'utf-8')
subject = '每日一文'
message['Subject'] = Header(subject, 'utf-8')
try:
qqmail.sendmail(account, receiver, message.as_string())
print('邮件发送成功')
except:
print('邮件发送失败')
qqmail.quit()
if __name__ == '__main__':
url = 'https://meiriyiwen.com'
data = getArticle(url)
send_email(data)Running the script sends the fetched article to the specified recipient; the result looks like the screenshot below.
You can also schedule the script with a task‑scheduler to receive daily reminders, turning the simple bot into a personal knowledge‑accumulation tool.
Conclusion
The article demonstrates a practical Python project that combines web scraping with automated email delivery, providing a reusable template for daily content sharing and personal automation.
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.
