python自动发QQ邮箱小程序

简介

        这是一个利用爬虫获取API接口数据,通过smtp协议发送邮件到QQ邮箱的小程序,系统设置定时发送。调用了几个接口,大概有以下这几个内容了,比如天气情况,十万个冷笑话,名言名句,冷知识等等,可以根据自己喜欢的内容来编写。

python自动发QQ邮箱小程序_第1张图片

程序

        爬取api数据之前要申请自己的api,一般都会有免费的 我用的是天行数据API。

contentSpider.py


import requests
import json
import time
# 每日一句
def dailyQuotes():
    url = 'http://api.tianapi.com/txapi/one/index?key=填入自己的申请key'
    res = requests.get(url)
    content = json.loads(res.text)['newslist'][0]['word']
    return '<每日一句>:\n' + content + '\n'

# 今日天气
def weatherApi():
    city = '湘潭市'
    url = 'http://api.tianapi.com/txapi/tianqi/index?key=填入自己的申请key&city='+city
    res = requests.get(url)
    content1 = json.loads(res.text)['newslist'][0]
    content2 = json.loads(res.text)['newslist'][1]
    weatherToday = {'area':city,'day':content1['week'],'temperature':content1['lowest']+'到'+content1['highest'],
                    '天气':content1['weather'],'tips':content1['tips']}
    weatherTomorrow = {'area': city, 'day': content2['week'], 'temperature': content2['lowest'] + '到' + content2['highest'],
                    '天气': content2['weather'], 'tips': content2['tips']}
    weatherInfo = '<天气预报>:\n' + '今天是'+weatherToday['day'] + ',气温范围:' + weatherToday['temperature'] + ',系统提醒您:' + weatherToday['tips'] \
                  + '\n明天是'+weatherTomorrow['day'] + ',气温范围:' + weatherTomorrow['temperature'] + ',系统提醒您:' + weatherTomorrow['tips']
    return weatherInfo

# 经典对联
def classicCouplet():
    url = 'http://api.tianapi.com/txapi/duilian/index?key=填入自己的申请key'
    data = {'key':填入自己的申请key}
    res = requests.get(url=url,params=data)
    content = json.loads(res.text)['newslist'][0]['content']
    return content

# 十万个为什么
def whyAndWhy():
    url = 'http://api.tianapi.com/txapi/tenwhy/index?key=填入自己的申请key'
    res = requests.get(url)
    title = json.loads(res.text)['newslist'][0]['title']
    answer = json.loads(res.text)['newslist'][0]['content']
    content = {'title':title,'answer':answer}
    return content

def main():
    date= time.strftime("%Y-%m-%d")
    # print(date)
    # 这里加延时是为了让系统识别为三次不同的请求
    whyAndWhy1 = whyAndWhy()
    time.sleep(1)
    whyAndWhy2 = whyAndWhy()
    time.sleep(1)
    whyAndWhy3 = whyAndWhy()

    contents = '今天是公元 ' + date + ',系统给您的播报内容如下:\n\n' + dailyQuotes() +'\n'+ weatherApi()+'\n\n<经典对联>:\n' \
            '' + classicCouplet() + '\n\n<十万个为什么1>:\n' + whyAndWhy1['title'] + '\n' + whyAndWhy1['answer'] + \
               '\n<十万个为什么2>:\n' + whyAndWhy2['title'] + '\n' + whyAndWhy2['answer'] + \
               '\n<十万个为什么3>:\n' + whyAndWhy3['title'] + '\n' + whyAndWhy3['answer']
    print(contents)
    return contents

main()

        使用smtp协议给qq邮箱发邮件,内容是上面爬虫程序获取到的数据, 导入模块 from content import main,调用mian()函数的返回值就是整理好的正文内容。

sendmail.py


# coding=utf-8
import smtplib
from email.mime.text import MIMEText
from content import main

msg_from = '[email protected]'  # 发送方邮箱
passwd = 'xxxxxxxxxxx'  # 填入发送方邮箱的授权码
msg_to = '[email protected]'  # 收件人邮箱

subject = "系统今日份播报"  # 主题
content = main()  # 正文是main()函数的返回值
msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = msg_from
msg['To'] = msg_to
try:
    s = smtplib.SMTP_SSL("smtp.qq.com", 465)  # 邮件服务器及端口号
    s.login(msg_from, passwd)
    s.sendmail(msg_from, msg_to, msg.as_string())
    print("发送成功")
except :
    print("发送失败")
finally:
    s.quit()

自动发邮件

        如何让系统自动发邮件可以在电脑中进行设置,具体可以百度windows如何自动执行python文件

        设置好了大概就是下面这样,可以设置触发器发送定时邮件,不过这种方法不理想,电脑不可能24小时开机(除非你用的云服务器),所以我改成了开机启动时发送,我想大家每天应该都会使用到电脑,这样就可以自动每天推送信息啦。
python自动发QQ邮箱小程序_第2张图片

总结

        这个程序使用到了python的爬虫库requests,解析库beatifulsoup,还有smtp协议,也学会了如何调用网络上各种付费免费的api。

你可能感兴趣的:(爬虫,python,python,smtp,爬虫,api)