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

Posted 菜鸟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 定时爬取内容并发送报告到指定邮箱相关的知识,希望对你有一定的参考价值。

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 定时爬取内容并发送报告到指定邮箱的主要内容,如果未能解决你的问题,请参考以下文章

26自动爬取每日的天气,并定时把天气数据和穿衣提示发送到你的邮箱

利用 Python 自动抓取微博热搜,并定时发送至邮箱

Python 定时发送每日天气和每日简报至邮件或钉钉(代码部署在云服务器)

Python 定时发送每日天气和每日简报至邮件或钉钉(代码部署在云服务器)

Python 定时发送每日天气和每日简报至邮件或钉钉(代码部署在云服务器)

Python3爬虫用Python实现发送天气预报邮件