抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

这是土土用 Python 发邮件的学习笔记

用 Python 推送每日天气

用到的 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']

#设置email信息
#邮件内容设置
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) #打印错误

用 Python 推兽图

青柠大佬在寒假写了一个每日推兽图的项目,
我突发奇想,通过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']

#设置email信息
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

评论