1 import smtplib , requests , schedule , time 2 from email.mime.text import MIMEText 3 from email.header import Header 4 from bs4 import BeautifulSoup 5 6 def course_spider(): 7 list_course = [] 8 url = 'http://www.xiachufang.com/explore/?page=' 9 headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'} 10 for i in range(1,4): 11 res_recipe = requests.get(url+str(i) , headers=headers) 12 res_recipe.encoding = 'utf-8' 13 bs_recipe = BeautifulSoup(res_recipe.text , 'html.parser') 14 list_recipe = bs_recipe.find(class_='normal-recipe-list').find_all('li') 15 for a_recipe in list_recipe: 16 course = a_recipe.find(class_='name') 17 ingredient = a_recipe.find(class_='ing ellipsis') 18 list_course.append('菜名:{}\n原料:{}\n..........\n'.format(course.text.strip() , ingredient.text.strip())) 19 course_content = ' '.join(list_course) 20 return course_content 21 22 23 def send_mail(): 24 qqmail = smtplib.SMTP() 25 qqmail.connect('smtp.qq.com' , 25) 26 qqmail.login('2191313025@qq.com' , 'tdkphuzskgwwebed') 27 content = course_spider() 28 message = MIMEText(content , 'plain' , 'utf-8') 29 message['Subject'] = Header('Happy new year' , 'utf-8') 30 31 qqmail.sendmail('2191313025@qq.com' , '2191313025@qq.com' , message.as_string()) 32 qqmail.quit() 33 34 def job(): 35 send_mail() 36 37 schedule.every(20).seconds.do(job) 38 while True: 39 schedule.run_pending() 40 time.sleep(2)