python爬取教务处课表 |
您所在的位置:网站首页 › python写课程表 › python爬取教务处课表 |
# coding=gbk import smtplib import urllib.request import requests import os from bs4 import BeautifulSoup from email.mime.text import MIMEText mail_host = 'smtp.163.com' #163用户名 mail_user = '************' #密码(部分邮箱为授权码) mail_pass = '************' #邮件发送方邮箱地址 sender = '*******@163.com' #邮件接受方邮箱地址,注意需要[]包裹,这意味着可以写多个邮件地址群发 receivers = ['*******@qq.com'] url='http://***************'#爬取的网站这里用‘*’代替 headers={ 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Encoding':'gzip, deflate', 'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2', 'Cache-Control':'max-age=0', 'Connection':'keep-alive', 'Cookie':'*******************; CERLOGIN=M8wqKKTB1b%2Fp%2Bnl6YgP7uNF9v0HofZpUTj92pLdVHwzr8a5zewy8K9vuKdE5BUak0PXsO9ehXJbX%0ACqP3MjymeFpISgt3DlihuAF6mdj4sTKB1F08G02AyvdbE1q0plmGZOHsYuWrMVchT5Lm6J7rks7R%0AFb4xrGJdO2ghLF0%2Bz8c%3D%0A%23dfnJiDCWXARjpFlxTIZ1F8Mp%2F0GI70%2BbzRw2TogHFmu10Jx8a%2BbACVeqh4pbu0VlRh1Qgg20pn5Y%0ACo1w%2FYorz0c%2FuZzvRwbzYZ4r9wBANG1TOC8jPomxouiYO7JOqjoPNEa%2B%2BFEAFz99bPlrqrsb5sXA%0A6Lfa1************', 'Host':'************', 'Referer':'http://*********/Certification/', 'Upgrade-Insecure-Requests':'1', 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Chrome/69.0' }#header 信息 page = requests.get(url,headers=headers) html=page.text soup = BeautifulSoup(html, 'html.parser') titles = soup.find_all(style="margin-top: 2px; font-size: 10px; color: red") with open(r"C:\Users\Administrator\kebiao.txt", "w",encoding='gbk') as file: for title in titles: file.write(title.text) with open(r"C:\Users\Administrator\kebiao.txt") as file_obj: contents = file_obj.read() message = MIMEText(contents.replace(' ',''),'plain','utf-8') #这一部分是文字处理,我是先保存在文件夹中,再从文件中提取字符发送到邮箱 message['Subject'] = '***的课表' message['From'] = sender message['To'] = receivers[0] try: smtpObj = smtplib.SMTP() smtpObj.connect(mail_host,25) smtpObj.login(mail_user,mail_pass) smtpObj.sendmail( sender,receivers,message.as_string()) smtpObj.quit() print('success')#成功 except smtplib.SMTPException as e: print('error',e)#失败 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |