定时爬取下厨房菜谱并邮件发送

import requests
import smtplib
import schedule
import time
from bs4 import BeautifulSoup
from email.mime.text import MIMEText
from email.header import Header

account = “"#替换发送邮箱
#input(‘请输入你的邮箱:’)
password = “邮箱授权码”
#input(‘请输入你的密码:’)
receiver = "
”#替换接收邮箱
#input(‘请输入收件人的邮箱:’)

def recipe_spider():
headers = {
‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36’
}
res_foods = requests.get(‘http://www.xiachufang.com/explore/’,headers=headers)
bs_foods = BeautifulSoup(res_foods.text,‘html.parser’)
list_foods = bs_foods.find_all(‘div’,class_=‘info pure-u’)
list_all = ‘’
num=0
for food in list_foods:
num=num+1
tag_a = food.find(‘a’)
name = tag_a.text.strip()
url = ‘http://www.xiachufang.com’+tag_a[‘href’]
tag_p = food.find(‘p’,class_=‘ing ellipsis’)
ingredients = tag_p.text.strip()
food_info = ‘’’
序号: %s
菜名: %s
链接: %s
原料: %s
‘’’%(num,name,url,ingredients)
list_all=list_all+food_info
return(list_all)

def send_email(list_all): #定义邮件发送
smtp_server = “smtp.sina.com” #发信服务器
text = ‘亲爱的,本周的热门菜谱如下’+list_all
msg = MIMEText(text,“plain”,“utf-8”)
subject_w = “周末吃啥”
#邮件头信息
msg[“From”] = Header(account)
msg[“to”] = Header(receiver)
msg[“subject”] = Header(subject_w,“utf-8”)
#开启发信服务
server = smtplib.SMTP_SSL(smtp_server)
server.connect(smtp_server,465)
#登录发信邮箱
server.login(account, password)
try:
server.sendmail(account, receiver, msg.as_string())
print (‘邮件发送成功’)
except:
print (‘邮件发送失败’)
server.quit()

def job():
print(‘开始一次任务’)
list_all = recipe_spider()
send_email(list_all)
print(‘任务完成’)

schedule.every().day.at(“18:27”).do(job)#部署每周三的13:15执行函数的任务

while True:
schedule.run_pending()
time.sleep(1)

------------------------------------------------致力简洁易懂Python3.7--------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值