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--------------------------------------------