Build a Python Web Scraper that Emails Daily Articles Automatically

This tutorial shows how to create a Python web scraper that fetches a daily article, formats it, and sends it via email using SMTP, including the full source code and step‑by‑step implementation details.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Build a Python Web Scraper that Emails Daily Articles Automatically

Introduction

Hello, I am a Python enthusiast sharing a useful script I found in a Python group that automatically sends a daily article via email.

Implementation Idea

The idea consists of two parts: first, a Python web scraper that retrieves the daily article from a web page; second, an email‑sending routine that formats the article and delivers it to a specified recipient.

Implementation Process

The complete source code is shown below.

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]), '

', str('
'.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)

Replace the placeholders with your email address, email authorization code, and the recipient's address. For detailed configuration steps, refer to the guide at https://mp.weixin.qq.com/s?__biz=MzU3MzQxMjE2NA==∣=2247498232&idx=1&sn=18e473486a4969973fc6b49b50f69059.

After running the script, the recipient will receive an email containing the fetched article, as shown below.

You can also schedule the script to run daily using a task scheduler, turning it into a personal daily reading reminder.

Conclusion

This article demonstrates a small project that combines Python web scraping and automated email sending to deliver a daily article automatically.

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.

SchedulingrequestsSMTPemail automationlxml
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.