Python爬虫爬取菜谱并邮件定期发送

#! /user/bin/env python
# -*- encoding: utf-8 -*-
#@ filename     :html10_job1.py
#@ description  :下厨房网站中的“本周最受欢迎菜谱”,让程序在每个周五爬取数据,并把菜谱发送到我们的邮箱。
#               http://www.xiachufang.com/explore/
#@ Author       :Dannie
#@ time         :2020/08/10 23:39:41
#@ version      :V1.1
#@ update note  :更新list转str方式,由文件存储提取改为.join函数调用
#********************#

import requests
from bs4 import BeautifulSoup

import smtplib
from email.mime.text import MIMEText
from email.header import Header

import schedule
import time

# def list_to_str(lists):    
#     file1 = open('recipe.txt','w',encoding='utf-8')
#     for listi in lists:
#         file1.writelines(listi)
#         file1.write('\n')
#     file1.close()
#     with open('recipe.txt','r',encoding='utf-8') as file:
#         lines = file.readlines()
#         strlines = ''.join(lines) 
#     return strlines

#爬取数据
def get_recipe():
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
    url = 'http://www.xiachufang.com/explore/'
    url2 = 'http://www.xiachufang.com/'
    res = requests.get(url,headers=headers)
    print(res.status_code)
    res_soup = BeautifulSoup(res.text,'html.parser')
    recipes = res_soup.find_all('div',class_='info pure-u')

    recipes_info = []
    i = 1
    for recipe in recipes:
        item = str(i)
        title = recipe.find('a').text
        recipe_url = url2 + recipe.find('a')['href']
        composition = recipe.find('p',class_='ing ellipsis').text
        # recipes_info.append([item,title,recipe_url,composition])
        data_recipe = '\n'.join([item,title,recipe_url,composition])   #将list转换为str
        recipes_info.append(data_recipe)
        i += 1
    # data_recipes = list_to_str(recipes_info)
    data_recipes = '\n'.join(recipes_info)     #将list转换为str
    return data_recipes

#发送邮件
def send_mail(content):
    mailhost = 'smtp.qq.com'
    server = smtplib.SMTP_SSL(host=mailhost)
    server.connect(mailhost,465)

    #私密信息,可以使用input()函数作为全局变量输入
    from_addr = ''    #邮箱账号
    password = ''    #邮箱授权密码
    to_addr = ''  #收件邮箱
    
    server.login(from_addr,password)
    
    message = MIMEText(content,'plain','utf-8')    #MIMEText只能发送文本格式,字符串/HTML
    subject = '本周最受欢迎菜谱'
    message['From'] = from_addr
    message['To'] = to_addr
    message['Subject'] = Header(subject,'utf-8')
    # 发送邮件,退出邮件
    try:
        server.sendmail(from_addr,to_addr,message.as_string())
        print('邮件发送成功')
        flag = 1
    except:
        print('邮件发送失败')
        flag = 0
    server.quit()
    return flag

def job():
    content = get_recipe()
    flag = send_mail(content)
    if flag == 1:
        print('本周菜谱发送成功')
    else:
        print('本周菜谱发送失败')


if __name__ == '__main__':
    # job()
    #定时发送
    schedule.every().friday.at("13:15").do(job)
    while True:
        schedule.run_pending()
        time.sleep(1)

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值