用到的 api : https://tianqiapi.com/
get_weather.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import re from datetime import datetime from os import error from urllib import request, error import ssl ssl._create_default_https_context = ssl._create_unverified_context try: resp = request.urlopen("https://tianqiapi.com/api.php?style=tc&skin=pitaya") html = resp.read().decode("utf-8") weather = re.findall("<em>.+.</em>", html)[0] print(weather) print("okk") except error.HTTPError as e: print("error:{}".format(e.code))
|
sent_email.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| import smtplib from email.mime.text import MIMEText import get_weather import time
mail_host = '`smtp.qiye.aliyun.com`'
mail_user = '`tutu@hifurry.cn`'
mail_pass = 'your_email_password'
sender = '`tutu@hifurry.cn`'
receivers = ['xxx@xxx.com']
message = MIMEText(get_weather.weather ,'html','utf-8')
message['Subject'] = '{}-今日天气'.format(time.strftime("%Y/%m/%d", time.localtime()))
message['From'] = sender
message['To'] = receivers[0]
try: smtpObj = smtplib.SMTP() smtpObj.connect(mail_host,25) smtpObj.login(mail_user,mail_pass) smtpObj.sendmail( sender,receivers,message.as_string()) smtpObj.quit() print('success') except smtplib.SMTPException as e: print('error',e)
|
青柠大佬在寒假写了一个每日推兽图的项目,
我突发奇想,通过py爬虫,自动将图发送到邮箱,
get_furry_img.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import re from datetime import datetime from os import error from urllib import request, error import ssl import time ssl._create_default_https_context = ssl._create_unverified_context try: resp = request.urlopen("https://furry.lihouse.xyz") html = resp.read().decode("utf-8") pic_url = re.findall("<img class=\"full-bg\".+.jpeg\">", html)[0] pic_url = "<style>img{{width:500px;}}</style>{}<p>查看图片详情:https://furry.lihouse.xyz/index.php?ftime={}".format(pic_url, time.strftime("%Y%m%d", time.localtime())) print(pic_url) print("okk")
except error.HTTPError as e: print("error:{}".format(e.code))
|
sent_emall.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import smtplib from email.mime.text import MIMEText import get_furry_img import time
mail_host = '`smtp.qiye.aliyun.com`' mail_user = '`tutu@hifurry.cn`' mail_pass = 'your_email_password' sender = '`tutu@hifurry.cn`' receivers = ['xxx@xxx.com']
message = MIMEText(get_furry_img.pic_url ,'html','utf-8') message['Subject'] = '{}-今日兽兽推送'.format(time.strftime("%Y/%m/%d", time.localtime())) message['From'] = sender message['To'] = receivers[0]
try: smtpObj = smtplib.SMTP() smtpObj.connect(mail_host,25) smtpObj.login(mail_user,mail_pass) smtpObj.sendmail( sender,receivers,message.as_string()) smtpObj.quit() print('success') except smtplib.SMTPException as e: print('error',e)
|
。。。就是这样一篇水水的文章
参考资料:https://zhuanlan.zhihu.com/p/24180606