码迷,mamicode.com
首页 > 编程语言 > 详细

python 定时爬取内容并发送报告到指定邮箱

时间:2019-12-17 20:29:12      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:content   内容   输入   schedule   pid   失败   eve   input   sts   

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)

 

python 定时爬取内容并发送报告到指定邮箱

标签:content   内容   输入   schedule   pid   失败   eve   input   sts   

原文地址:https://www.cnblogs.com/houdj/p/12056366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!