QQ邮箱发送 天气状况及 警句
今天小编学习了 qq邮箱发送邮件 及smtp服务,首先需要在qq邮箱中开启smtp服务。
首先,需要进入qq邮箱中的设置,找到账户,找到相关服务
选择开启POP3/SMTP服务,会获取到一个授权码。在连接的时候需要用授权码进行登录。
因为需要发送天气状况,因此需要获取天气状况,小编也是知道一个网址,没有什么反爬措施,直接用requests库进行获取就可以获取天气状况。
link: http://wthrcdn.etouch.cn/weather_mini?city=
子需要在后面加上city就可以获取天气状况。
如图获取到了 长春 的天气状况,包括昨天的以及未来一周的天气。
下面来构造 获取天气信息的代码
def getweather(city, link):
url = link + city
# url = data['link'] + data['city']
resp = requests.get(url).json()
yesterday = resp['data']['yesterday']
forecast1 = resp['data']['forecast'][0]
forecast2 = resp['data']['forecast'][1]
msg = '\n' + '亲爱的 佳旺! 今明两天天气状况是 :\n\n' + '\t' + forecast1['date'] \
+ '\t最高温:' + forecast1['high'] + '\t 最低温' + forecast1['low'] + '\t' \
+ forecast1['type'] + ' 风力' + forecast1['fengli'][9:-3] + '\n\n\t' + forecast2['date'] + '\t最高温:' \
+ forecast2['high'] + '\t 最低温' + forecast2['low'] + '\t' + forecast2['type'] + ' 风力' + forecast2['fengli'][9:-3]
return msg
利用json的索引。
接下来利用金山词霸的接口 获取每日一句。
http://open.iciba.com/dsapi/
def getword(link):
res = requests.get(link).json()
msg = res['content'] + '\n' + res['note']
return str(msg)
最后利用 smtp服务进行发送邮件的服务。
if __name__ == '__main__':
# getweather(data['city'], data['link'])
msg = data['first'] + getweather(data['city'], data['link']) \
+ '\n\n' + getword(data['link1']) + data['last']
message = """
From: From 专属 贾维斯-*** <1091899349@qq.com>
To:To person <a---runner@qq.com>
Subject:尊敬的主人,请点击查收!
This is a e-mail message.""" + '\n' + msg
send_msg = MIMEText(message)
send_msg['Subject'] = '每日问候'
send_msg['From'] = '***专属 贾维斯'
try:
# 发送邮件
smtp = smtplib.SMTP()
smtp.connect('smtp.qq.com', 25)
smtp.login('1091*****@qq.com', '你的授权码')
smtp.sendmail('1091*****@qq.com', '1091*****@qq.com', send_msg.as_string())
print('发送成功!!!')
except Exception as e:
print('发送失败!!!')
finally:
smtp.quit()
这是发送 的内容界面。
下面附上完整的艾玛。
import requests, smtplib, time
from email.mime.text import MIMEText
data ={
'link': 'http://wthrcdn.etouch.cn/weather_mini?city=',
'city': '长春',
'link1': 'http://open.iciba.com/dsapi/',
'first': '小可爱 上线啦!!!',
'last': '\r\n\r\n'
}
def getweather(city, link):
url = link + city
# url = data['link'] + data['city']
resp = requests.get(url).json()
yesterday = resp['data']['yesterday']
forecast1 = resp['data']['forecast'][0]
forecast2 = resp['data']['forecast'][1]
msg = '\n' + '亲爱的 佳旺! 今明两天天气状况是 :\n\n' + '\t' + forecast1['date'] \
+ '\t最高温:' + forecast1['high'] + '\t 最低温' + forecast1['low'] + '\t' \
+ forecast1['type'] + ' 风力' + forecast1['fengli'][9:-3] + '\n\n\t' + forecast2['date'] + '\t最高温:' \
+ forecast2['high'] + '\t 最低温' + forecast2['low'] + '\t' + forecast2['type'] + ' 风力' + forecast2['fengli'][9:-3]
return msg
'''
另一种写法:
import json
resp = requests.get(url)
return json.loads(resp.text)
'''
def getword(link):
res = requests.get(link).json()
msg = res['content'] + '\n' + res['note']
return str(msg)
if __name__ == '__main__':
# getweather(data['city'], data['link'])
msg = data['first'] + getweather(data['city'], data['link']) \
+ '\n\n' + getword(data['link1']) + data['last']
message = """
From: From 专属 贾维斯-刘佳旺 <1091899349@qq.com>
To:To person <a---runner@qq.com>
Subject:尊敬的主人,请点击查收!
This is a e-mail message.""" + '\n' + msg
send_msg = MIMEText(message)
send_msg['Subject'] = '每日问候'
send_msg['From'] = '刘佳旺专属 贾维斯'
print(message)
try:
# 发送邮件
smtp = smtplib.SMTP()
smtp.connect('smtp.qq.com', 25)
smtp.login('1091899349@qq.com', 'dnsnavjpgkvshegc')
smtp.sendmail('1091899349@qq.com', '1091899349@qq.com', send_msg.as_string())
print('发送成功!!!')
except Exception as e:
print('发送失败!!!')
finally:
smtp.quit()