python 爬取教务处通知

内容很简单,也没有难度
存下代码
(教务处主页更新后有点毛病

from datetime import datetime
from urllib.request import urlopen
from bs4 import BeautifulSoup
import time

#函数
def getTitle():
    global titleSet
    global todaysTitle
    html = urlopen("http://jwc.ahu.cn/main/index.asp")
    bsObj = BeautifulSoup(html.read(),"lxml")
    All_title = bsObj.findAll("td",{"height":"22"})
    for title in All_title:
        if 'title' in title.attrs:
            if title.attrs['title'] not in titleSet:
                titleTime=title.find("font",{"color":"#999999"}).get_text()
                if titleTime == dateToday:
                    todaysTitle=title.attrs['title']
                else:
                    print(titleTime + title.attrs['title'])
                    titleSet.add(title.attrs['title'])

#存储出现过标题的集合set

titleSet = set()
todaysTitle = ''
titleSet.add(todaysTitle)
dateToday = datetime.now().strftime('(%Y/%m/%d)')
#修改格式与教务处保持一致
if dateToday[9] == '0':
    dateToday = dateToday[:9] + dateToday[10:]
if dateToday[6] == '0':
    dateToday = dateToday[:6] + dateToday[7:]
#第一次加载
getTitle()
print('Load completely...waiting for news\n')
if todaysTitle not in titleSet:
    titleSet.add(todaysTitle)
    print('Today:\n'+dateToday + todaysTitle)

hour = datetime.now().strftime('%H')
while 1:
    #小时数变更的时候打印下 (证明程序没有卡死 hhh
    newHour = datetime.now().strftime('%H')
    if hour != newHour:
        print(newHour+':00')
        hour = newHour

    getTitle()
    if todaysTitle not in titleSet:
        titleSet.add(todaysTitle)
        print('Today:\n'+dateToday + todaysTitle)



    time.sleep(1800)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Python中的requests库来爬取公告通知的内容,然后使用smtplib库来发送邮件。下面是一个简单的示例代码: ```python import requests import smtplib from email.mime.text import MIMEText def send_mail(subject, body): # 设置发件人和收件人 sender = '[email protected]' receiver = '[email protected]' # 设置SMTP服务器地址和端口号 smtp_server = 'smtp.example.com' smtp_port = 587 # 设置发件人邮箱的用户名和密码 username = '[email protected]' password = 'your_password' # 创建邮件内容 msg = MIMEText(body) msg['Subject'] = subject msg['From'] = sender msg['To'] = receiver try: # 连接SMTP服务器并登录 server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() server.login(username, password) # 发送邮件 server.sendmail(sender, receiver, msg.as_string()) print('邮件发送成功') except Exception as e: print('邮件发送失败:', str(e)) finally: # 关闭连接 server.quit() def crawl_announcement(url): try: # 发起HTTP请求并获取页面内容 response = requests.get(url) if response.status_code == 200: # 解析页面内容并提取公告通知 announcements = response.json() # 假设返回的是JSON格式数据,具体根据实际情况修改解析方式 return announcements else: print('请求失败,状态码:', response.status_code) except Exception as e: print('请求出错:', str(e)) # 爬取公告通知 announcements = crawl_announcement('http://example.com/announcements') if announcements: # 构造邮件内容 subject = '最新公告通知' body = '\n'.join(announcements) # 发送邮件 send_mail(subject, body) ``` 以上代码中,`crawl_announcement`函数用于爬取公告通知的内容,`send_mail`函数用于发送邮件。你需要根据实际情况修改URL、发件人和收件人的邮箱地址,SMTP服务器的地址和端口号,以及发件人邮箱的用户名和密码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值