Python實作在遠端服務器掛代碼—發送定時天氣預報至郵箱+每日一句(小白教程)

人生苦短,我用python,下面來講解一個python每天定時發送天氣預報和每日一句至指定郵箱的腳本掛在服務器運行的程式,
廢話不多說,先來看一眼整體代碼,再來一步一步講解,
import requests
import smtplib
import schedule
import time
from bs4 import BeautifulSoup
from email.mime.text import MIMEText
from email.header import Header
account =' xxxxxxx@qq.com'#**這里填發件人郵箱,我用的是QQ郵箱**,
password = 'xxxxx'#**這里填發件人的授權碼!不是登錄密碼!**
receiver ='xxxxxx @qq.com'#**這里填收件人的郵箱,**
def sentence():
url = "http://open.iciba.com/dsapi/"
r = requests.get(url)
content = r.json()['content']
note = r.json()['note']
return content,note
def weather_spider():
headers={'xxxx'}#偽裝頭
url='http://www.weather.com.cn/weather1d/XXXXX.shtml'#填寫天氣網站所在地的網址
res=requests.get(url=url,headers=headers)
res.encoding='utf-8'
soup=BeautifulSoup(res.text,'html.parser')
tem1= soup.find(class_='tem')
weather1= soup.find(class_='wea')
clothes1=soup.find(class_='li3 hot').find('p')
tem=tem1.text
weather=weather1.text
clothes=clothes1.text
return tem,weather,clothes
def send_email(tem,weather,clothes,content,note):
mailhost = 'smtp.qq.com'##把qq郵箱的服務器地址賦值到變數mailhost上,地址應為字串格式
qqmail = smtplib.SMTP_SSL(mailhost)##實體化并連接SMTP埠號,
qqmail.connect(mailhost,465)#**如果放在阿里云服務器運行,只能465埠,25埠會被當做垃圾郵件,**
qqmail.login(account,password)
content= '親,今日天氣: '+tem+', '+weather+', '+clothes+' '+content+' '+note
message = MIMEText(content, 'plain', 'utf-8')
subject = ' 標題'#***這里寫郵件的標題***
message['Subject'] = Header(subject, 'utf-8')
try:
qqmail.sendmail(account, receiver, message.as_string())
print ('郵件發送成功')
except:
print ('郵件發送失敗')
qqmail.quit()
def job():
print('開始一次任務')
content,note=sentence()
tem,weather,clothes= weather_spider()
send_email(tem,weather,clothes,content,note)
print('任務完成')
schedule.every().day.at("07:00").do(job)#定時七點執行任務,
while True:
schedule.run_pending()
time.sleep(1)#檢查部署的情況,如果任務準備就緒,就開始執行任務,
一、完成代碼
1、安裝需要的庫
這三個屬于第三方庫,需要另行下載
requests Win10在cmd命令輸入 pip install request
BeautifulSoup4 pip install BeautifulSoup4
schedule pip install schdeduile
2、填寫發收件人資訊
ccount =' xxxxxxx@qq.com'#**這里填發件人郵箱,我用的是QQ郵箱**,
password = 'xxxxx'#**這里填發件人的授權碼**
receiver ='xxxxxx @qq.com'#**這里填收件人的郵箱,**
QQ郵箱
這個password需要我們去到這里獲取:請打開https://mail.qq.com/,登錄你的郵箱,然后點擊位于頂部的【設定】按鈕,選擇【賬戶設定】,然后下拉到這個位置,
若為關閉,則開啟POP3服務()
會收到一個授權碼,即為password,
二、定義函式
——1(每日一句爬取)
def sentence():
url = "http://open.iciba.com/dsapi/"
r = requests.get(url)
content = r.json()['content']
note = r.json()['note']
return content,note
——2(天氣預報爬取)
def weather_spider():
headers={'xxxx'}#偽裝頭
url='http://www.weather.com.cn/weather1d/101221201.shtml'#替換為自己所在地方網址
res=requests.get(url=url,headers=headers)
res.encoding='utf-8'
soup=BeautifulSoup(res.text,'html.parser')
tem1= soup.find(class_='tem')
weather1= soup.find(class_='wea')
clothes1=soup.find(class_='li3 hot').find('p')
tem=tem1.text
weather=weather1.text
clothes=clothes1.text
return tem,weather,clothes
墨跡天氣

——3(發送郵件)
def send_email(tem,weather,clothes,content,note):
mailhost = 'smtp.qq.com'
qqmail = smtplib.SMTP_SSL(mailhost)
qqmail.connect(mailhost,465)#**如果放在阿里云服務器運行,只能465埠,25埠會被當做垃圾郵件,**
qqmail.login(account,password)
content= '今日天氣: '+tem+', '+weather+', '+clothes+' '+content+' '+note
message = MIMEText(content, 'plain', 'utf-8')
subject = ' 標題'#***這里填寫郵件的標題***
message['Subject'] = Header(subject, 'utf-8')
try:
qqmail.sendmail(account, receiver, message.as_string())
print ('郵件發送成功')
except:
print ('郵件發送失敗')
qqmail.quit()
——4(執行任務)
def job(): #創建任務函式
print('開始一次任務')
content,note=sentence()
tem,weather,clothes= weather_spider()
send_email(tem,weather,clothes,content,note)
print('任務完成')
schedule.every().day.at("07:00").do(job)#定時七點執行任務,
while True:
schedule.run_pending()
time.sleep(1) #檢查部署的情況,如果任務準備就緒,就開始執行任務,
——5小結
到這里代碼就完成了

只要電腦開著,腳本就會一直運行下去, 在本地運行顯示這樣就是成功了, 但這樣太麻煩,誰大清早起來開機運行電腦,
可以用服務器遠程掛著腳本,讓他自動運行, 接下來來講如何做,
三、 服務器
1、搭建服務器
在這里推薦使用阿里云,因為相比其他服務器穩定且簡便.
阿里云網
阿里云有開發者計劃,專門針對學生的優惠,首次可以試用免費服務器,

我用的是輕量應用級服務器,便宜而且實用,有學生優惠,其他免費試用的,也可以用,

找到自己的服務器,設定密碼并記住,

記住自己的IP 和賬號名:adminstrator
2、建立ftp站點
——1
在電腦中下載FileZilla
依次 輸入主機(即剛才的IP),用戶名(即adminstrator),密碼,埠不輸入也可(默認21)
——2
在服務器端完成檔案中操作
建立FTP站點檔案
之后防火墻添加規則如下圖

3、建立python環境
1、服務器搭建python環境檔案
2、下載pip檔案
之后在服務器端cmd依次安裝需要的庫,具體參照第開頭第一步,
requests
BeautifulSoup4schedule
總結
點擊運行,之后這個程式就在服務器一直運行了!每天定時發送(我用的是vscode運行)
創作不易,謝謝支持!

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/229201.html
標籤:python
下一篇:作業中的一些小總結(2)

