基於遠程服務器:Python實現每天給女朋友發送QQ郵件,信息包括天氣
哎,說到女朋友也慚愧,受疫情影響情人節也開始“雲”體驗,什麼東西也沒送,也沒陪人家。
事後我鋼筋直男想了想,給女朋友寫個腳本吧,畢竟自己是學這個的,那就寫個發QQ郵箱的腳本吧,每天給他發QQ郵箱,告訴她“多喝熱水,多穿衣服”
😂想想也香香,情人芳心也得了,學習的目的也達到了。幹!
一、首先獲得QQ郵箱授權
- 打開QQ郵箱
- 打開設置,點賬戶
- 下滑找到POP/SMTP服務,並打開,點擊生成授權碼,這個很重要
- 手機授權即可獲得授權碼
二、發送郵件代碼
上代碼(郵件發送篇)
首先用到第三方庫smtplib
pip install smtplib
import smtplib
from email.mime.text import MIMEText
from email.header import Header
def set_pic_file(text):
print ("郵件發送中")
mail_host = "smtp.qq.com" # 設置服務器
mail_user = "你的[email protected]" # 用戶名
mail_pass = "你的授權碼" # 口令,QQ郵箱是輸入授權碼,在qq郵箱設置 裏用驗證過的手機發送短信獲得,不含空格
sender = '你的[email protected]'
receivers = ['接收者[email protected]'] # 接收郵件,可設置爲你的QQ郵箱或者其他郵箱
message = MIMEText(text, 'plain', 'utf-8')#text是你要寫的文本內容
message['From'] = Header("你的[email protected]", 'utf-8')
message['To'] = Header("接收者[email protected]", 'utf-8')
subject ='今天依然愛你哦'
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
smtpObj.quit()
print ("郵件發送成功")
except smtplib.SMTPException as e:
print (e)
三、API接口調用
其實可以爬蟲的,但是爬蟲會讓代碼時間複雜度提高(其實就是懶),所以這裏選擇調用API接口。
API接口選擇
https://www.tianqiapi.com/index/doc
上代碼
import time
import requests
import ast
def get_url_comment(url):
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3741.400 QQBrowser/10.5.3863.400"
}
try:
r = requests.get(url ,headers = headers, timeout = 30)#獲得url的相關參數
r.raise_for_status()
r.encoding = r.apparent_encoding
print ("網頁獲取成功",r.status_code )
return r.text
except Exception as e :
return "網頁爬取異常" , r.status_code ,e#返回狀態碼
def weather():
a = get_url_comment("https://tianqiapi.com/api?version=v6&appid=15186356&appsecret=swW9lQmQ")
b = ast.literal_eval(a)
c = b["date"] + "\t" + b["week"] + "\t" + b["city"] + "\n" + b["wea"] + "\n最高氣溫 " + b["tem1"] + "℃" \
+ " --最低氣溫 " + b["tem2"] + "℃\n" + b["win"] + b["win_speed"] + "\n空氣質量 " + b["air"] + "\t空氣PM2.5指數 " \
+ b["air_pm25"] + "\t空氣水平 " + b["air_level"] + "\t" + b["air_tips"]
if int(b["tem2"]) <= 0 and int(b["tem1"]) >= 10:
d = "\n早安寶貝😘,早晨傍晚會比較冷,晝夜溫差大,記得早晚添衣保暖哦!🐷"
elif int(b["tem2"]) >= 0 and int(b["tem2"]) <= 6 and int(b["tem1"]) >= 10:
d = "\n早安寶貝😘,早晚都不算太冷,但記得添衣哦!😁"
elif int(b["tem2"]) >= 6 and int(b["tem1"]) >= 10:
d = "\n早安寶貝😘,天氣轉暖,寶貝,開心的一天啊!☺"
elif int(b["tem2"]) <= -5 and int(b["tem1"]) <= 10:
d = "\n早安寶貝😘,天氣有點涼,注意保暖哦!❤"
return c+d
針對天氣溫度,可以有自己的文本,我這裏懶得爬蟲寫情話了,都是自己一個一個敲上去的,嘻嘻,有女朋友的都懂,你請自寫的纔是最好的。
四、總代碼
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time
import requests
import ast
def get_url_comment(url):
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3741.400 QQBrowser/10.5.3863.400"
}
try:
r = requests.get(url ,headers = headers, timeout = 30)#獲得url的相關參數
r.raise_for_status()
r.encoding = r.apparent_encoding
print ("網頁獲取成功",r.status_code )
return r.text
except Exception as e :
return "網頁爬取異常" , r.status_code ,e#返回狀態碼
def weather():
a = get_url_comment("https://tianqiapi.com/api?version=v6&appid=15186356&appsecret=swW9lQmQ")
b = ast.literal_eval(a)
c = b["date"] + "\t" + b["week"] + "\t" + b["city"] + "\n" + b["wea"] + "\n最高氣溫 " + b["tem1"] + "℃" \
+ " --最低氣溫 " + b["tem2"] + "℃\n" + b["win"] + b["win_speed"] + "\n空氣質量 " + b["air"] + "\t空氣PM2.5指數 " \
+ b["air_pm25"] + "\t空氣水平 " + b["air_level"] + "\t" + b["air_tips"]
if int(b["tem2"]) <= 0 and int(b["tem1"]) >= 10:
d = "\n早安寶貝😘,早晨傍晚會比較冷,晝夜溫差大,記得早晚添衣保暖哦!🐷"
elif int(b["tem2"]) >= 0 and int(b["tem2"]) <= 6 and int(b["tem1"]) >= 10:
d = "\n早安寶貝😘,早晚都不算太冷,但記得添衣哦!😁"
elif int(b["tem2"]) >= 6 and int(b["tem1"]) >= 10:
d = "\n早安寶貝😘,天氣轉暖,寶貝,開心的一天啊!☺"
elif int(b["tem2"]) <= -5 and int(b["tem1"]) <= 10:
d = "\n早安寶貝😘,天氣有點涼,注意保暖哦!❤"
return c+d
def set_pic_file(text):
print ("郵件發送中")
mail_host = "smtp.qq.com" # 設置服務器
mail_user = "[email protected]" # 用戶名
mail_pass = "授權碼" # 口令,QQ郵箱是輸入授權碼,在qq郵箱設置 裏用驗證過的手機發送短信獲得,不含空格
sender = '[email protected]'
receivers = ['接收者@qq.com'] # 接收郵件,可設置爲你的QQ郵箱或者其他郵箱
message = MIMEText(text, 'plain', 'utf-8')
message['From'] = Header("[email protected]", 'utf-8')
message['To'] = Header("接收者@qq.com", 'utf-8')
subject ='今天依然愛你哦'#標題
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
smtpObj.quit()
print ("郵件發送成功")
except smtplib.SMTPException as e:
print (e)
if __name__ == '__main__':
c , d = 9 , 0
while True ://設置循環,在服務器後臺運行
a , b = time.localtime(time.time()).tm_hour,time.localtime(time.time()).tm_min
if a == c and b == d :
e = weather()
set_pic_file(e)
print(time.localtime(time.time()).tm_year,"年--",time.localtime(time.time()).tm_mon,"月",time.localtime(time.time()).tm_mday,"日--",a,":",b)
time.sleep(24*3600-120)
五、服務器
首先,你得購買服務器,我這裏選擇了華爲雲,開年有活動。
通過寶塔面板規格化服務器,通過Winscp(安裝包已上傳,可下載)來遠程連接服務器。
遠程連接時,服務器必須開放相關的出入規則。
開放好以後,就可以winscp遠程連接了。
登陸成功後,找到本地剛寫的dome文件,推拽到遠端。(記得遠端服務器配置python3)
好!至此,本地完成。
六、遠端操作
ls 列出 / 根目錄下的所有文件
cd 切換目錄到所保存程序的目錄下
cat 查看程序
記得創建一個日誌,來查看運行狀態。
執行命令nohup python -u emil.py > run.log 2>&1 &
託管到後臺。
七、最終效果
未來怎樣我不管,希望一路有你