功能

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

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. 利用固定格式发送电子邮件

实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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()