python自动抓取某网站食谱,并以excel文件的形式通过邮箱定时发送信息
2020-02-01 15:09
549 查看
python自动抓取某网站食谱,并以excel文件的形式通过邮箱定时发送信息
如题,本项目运用了requests,BeautifuSoup 库抓取数据,openpyxl库本地储存数据,email库各种模块设计并组成邮件,smtplib库第三方发送邮件,schedule库实现定时执行任务。代码如下:
import requests import openpyxl from bs4 import BeautifulSoup import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication from email.header import Header import schedule,time mailhost=input('你的邮箱号:') password=input('你的smtp授权码:') receiver=input('收件人邮箱号:') def food_spider(): res_foods = requests.get('http://www.xiachufang.com/explore/') bs_foods = BeautifulSoup(res_foods.text,'html.parser') list_foods = bs_foods.find_all('div',class_='info pure-u') list_all = [] for food in list_foods: tag_a = food.find('a') name = tag_a.text[17:-13] URL = 'http://www.xiachufang.com'+tag_a['href'] tag_p = food.find('p',class_='ing ellipsis') ingredients = tag_p.text[1:-1] list_all.append([name,URL,ingredients]) return(list_all) def save_data(data): wb=openpyxl.Workbook()#创建工作簿 sheet=wb.active#获取工作簿活动表 sheet.title='food'#title属性赋值 sheet['A1']='菜名'#单元格属性赋值 sheet['B1']='链接' sheet['C1']='食材' for i in data: sheet.append(i) wb.save('food.xlsx') def send_email(message): global mailhost,password,receiver qq_server='smtp.qq.com' try: smtp_obj=smtplib.SMTP() smtp_obj.connect(qq_server,25) smtp_obj.login(mailhost,password) smtp_obj.sendmail(mailhost,receiver,message.as_string()) smtp_obj.quit() print('发送成功') except smtplib.SMTPException as ex: print('发送失败:%s'%ex) def set_email(): message=MIMEMultipart('related')#采用related定义内嵌资源的邮件体 content='正文'#文本内容 textpart=MIMEText(content,'plain','utf-8')#文本部分实体化 file=open(r'D:\file-py\paqu\food.xlsx','rb')#打开本地文件 file_content=file.read()#读取附件部分所需信息 file.close()#关闭本地文件 xlsxpart=MIMEApplication(file_content)#附件部分实体化 xlsxpart.add_header('Content-Disposition','attachment',filename='food.xlsx')#附件都要带上附件声明 message.attach(textpart)#加上文本部分 message.attach(xlsxpart)#加上xlsx附件部分 message['Subject']=Header('下厨房-本周推荐美食')#加邮件头 主题 message['From']=Header('ZOU')#加邮件头 发件人 message['To']=Header('zou')# 加邮件头 收件人 return(message) def job(): print('开始任务') data=food_spider() save_data(data) message=set_email() send_email(message) print('任务结束\n') schedule.every().monday.at("8:00").do(job)#每周一早上八点自动发邮寄 while True: schedule.run_pending()#检查部署的情况,如果任务准备就绪,就开始执行任务(pending 即将发生的事) time.sleep(1)
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Python-爬取中国天气网天气并通过邮箱定时发送
- Android将日志信息自动发送到指定的邮箱中 邮件的内容以附件形式发送
- Android将日志信息自动发送到指定的邮箱中 邮件的内容以附件形式发送
- Android将日志信息自动发送到指定的邮箱中 邮件的内容以附件形式发送
- 通过python的paramiko抓取多台服务器信息,并通过html格式发送邮件到群组
- Android将日志信息自动发送到指定的邮箱中 邮件的内容以附件形式发送
- python使用requests和beautifusoup模块爬取学校网站的就业中心信息,并发送至自己的邮箱
- python实现获取天气信息后自动登录邮箱发送到指定邮件联系人
- 【Python】用Python获取天气并定时自动发送到邮箱
- .NET Core 实现定时抓取网站文章并发送到邮箱
- .NET Core 实现定时抓取博客园首页文章信息并发送到邮箱
- .NET Core 实现定时抓取博客园首页文章信息并发送到邮箱
- nodejs爬虫-通过抓取搜狗微信网站获取微信文章信息
- 通过python查找数据库内容自动发送excel邮件
- 【转】Oracle AWR 报告 每天自动生成并发送邮箱 Python脚本(一)
- python (18)在linux中如何实现定时发送邮件到指定邮箱,监测任务
- Python编写的命令行版微信。(已集成自动聊天机器人(通过网址api形式))
- [导入]C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- 通过curl抓取其他网站信息
- 自动抓取信息定时发布到微博