微信咨询

微信咨询

13610*910*9

服务热线 7*24小时

电话咨询

Python 自动抓取天气预报并且每天自动发送天气到指定邮箱

iamdu2019-12-11 10:03:16 浏览: 185240
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('请输入收件人的邮箱:')
account = '121942198@qq.com'
password = '' #填写QQ密码,非登录密码,详见QQ邮箱设置里
receiver = '76123708@qq.com'

def weather_spider():
    headers={'user-agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
    url='http://www.weather.com.cn/weather/101280601.shtml'
    res=requests.get(url,headers=headers)
    res.encoding='utf-8'
    soup=BeautifulSoup(res.text,'html.parser')
    tem1= soup.find(class_='tem')
    weather1= soup.find(class_='wea')
    tem=tem1.text
    weather=weather1.text
    return tem,weather

def send_email(tem,weather):
    mailhost='smtp.qq.com'
    qqmail = smtplib.SMTP_SSL(host='smtp.qq.com')
    qqmail.connect(mailhost,465)
    qqmail.login(account,password)
    content= tem+weather
    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('开始一次任务')
    tem,weather = weather_spider()
    send_email(tem,weather)
    print('任务完成')

schedule.every().day.at("10:00").do(job) 
while True:
    schedule.run_pending()
    time.sleep(1)

 

定时器的官方说明

欢迎留下你的看法

共 0 条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注