Python爬虫实战(二)教务处首页

功能

爬取四川大学教务处首页的通知,并邮件推送

github链接:https://github.com/ZhengLin-Li/leaning-spider-scu-jwc

思路

一、crawl.py

  1. 利用requests库,拿到页面的response对象
  2. 利用BeautifulSoup库解析拿到的文档树
  3. 把数据文件保存进data.txt文件中

二、send_email.py

  1. 引入SMTP,Header,MIMEText
  2. 利用固定格式发送电子邮件

实现

import requests
from bs4 import BeautifulSoup

response = requests.get('http://jwc.scu.edu.cn/')
page = response.content.decode()
# print(page)

soup = BeautifulSoup(page, 'lxml')
tag = soup.find_all(class_='list-llb-list')
# print(tag)

result = ''
for each in tag:
    result += each.text
# print(result)

with open('data.txt', 'w', encoding='utf8') as fp:
    for each in tag:
        text = each.text
        fp.write(text)
from smtplib import SMTP
from email.header import Header
from email.mime.text import MIMEText


def main():
    # 请自行修改下面的邮件发送者和接收者
    sender = 'xxxxxxxxx@qq.com'  # 发送者的邮箱地址
    receivers = ['xxxxxxxxxx@qq.com']  # 接收者的邮箱地址
    message = MIMEText(open('data.txt').read(), _subtype='plain', _charset='utf-8')
    message['From'] = Header('Your Old Friend', 'utf-8')  # 邮件的发送者
    message['To'] = Header('Darling Jay', 'utf-8')   # 邮件的接收者
    message['Subject'] = Header('To darling Jay', 'utf-8') # 邮件的标题
    smtper = SMTP('smtp.qq.com')
    # 请自行修改下面的登录口令

    smtper.login(sender, 'xxxxxxxxxxxxxxxx')  # QQ邮箱smtp的授权码
    smtper.sendmail(sender, receivers, message.as_string())
    print('邮件发送完成!')


if __name__ == '__main__':
    main()

我的邮箱:1125806272@qq.com
我的博客:http://9pshr3.coding-pages.com/
https://zhenglin-li.github.io/
我的csdn:https://me.csdn.net/Panda325
我的简书:https://www.jianshu.com/u/e2d945027d3f
我的今日头条:https://www.toutiao.com/c/user/4004188138/#mid=1592553312231438
我的博客园:https://www.cnblogs.com/zhenglin-li/

posted @ 2020-08-18 11:40  嗯这  阅读(157)  评论(0编辑  收藏  举报