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 weather_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) return list_all def send_email(content): mailhost=‘smtp.qq.com‘ qqmail = smtplib.SMTP() qqmail.connect(mailhost,25) qqmail.login(account,password) content= " ".join(content) message = MIMEText(content, ‘plain‘, ‘utf-8‘) subject = ‘今日菜谱‘ message[‘Subject‘] = Header(subject, ‘utf-8‘) try: qqmail.sendmail(account, receiver, message.as_string()) print (‘邮件发送成功‘) except: print (‘邮件发送失败‘) qqmail.quit() def job(): print(‘开始一次任务‘) con = weather_spider() send_email(con) print(‘任务完成‘) schedule.every().day.at("19:12").do(job) while True: schedule.run_pending() time.sleep(1)
原文地址:https://www.cnblogs.com/houdj/p/12056366.html
时间: 2024-10-06 20:13:31